Showing posts with label SQL 2005. Show all posts
Showing posts with label SQL 2005. Show all posts

Wednesday, January 23, 2013

Get a record or rowcount for all tables in a database

Received a request for a list of our tables and how many records each table had in it. 

In a situation like this you would want to avoid using the following logic as it actually goes through and counts each individual row.



SELECT count(*) FROM TableA
 

Instead using the system tables you can get an accurate and extremely fast list:






select t.name as TableName
     , i.rows as Records
    from     sysobjects as t
        JOIN sysindexes as i
            on i.id = t.id
    where t.xtype = 'U' and i.indid in (0,1)
order by TableName



table row count select statement for SQL Server













This query works for SQL Server 2000 and greater.




What is the current database name

It seems I've lost the ability to retain even the basic SQL information that I've learned over the years.  For the life of me I couldn't remember how to view what the current database name was. 

Fortunately Google has become my memory and my crutch. 



SELECT DB_NAME() AS DatabaseName

select db_Name() as DatabaseName - SQL Server











Wednesday, July 25, 2012

Find available space in a file group on SQL Server

Yesterday I attempted to add a new index to my development database and it returned an error stating that the file group didn't have enough disk space.  After a bit of searching I discovered the following code that would show the Total allocated and free MB worth of disk space in each file group.


-- check to make sure temp tables don't exist
IF (OBJECT_ID('tempdb..#fileStats') IS NOT NULL)
            drop table #fileStats

IF (OBJECT_ID('tempdb..#fileGroup') IS NOT NULL)
            drop table #fileGroup


-- Create temporary holding tables
create table #fileStats
    (
        fileID          int,
        fileGroup       int,
        totalExtents    int,
        usedExtents     int,
        name            varchar(255),
        fileName        varchar(1000)
    )

create table #fileGroup
    (
        groupid         int,
        groupname       varchar(256)
    )


-- Insert 
insert into #fileStats
    exec ('DBCC showfilestats with no_infomsgs')

-- Insert
insert into #fileGroup
    select  groupid, groupname
    from sysfilegroups

-- Get resulting set
-- size returned from showfilestats is in Extents -> 1 Extent is 64kb
-- so we need to multiply the sum of TotalExtents by 64 and then divide by 1024 to get
-- the actual MB

select  g.groupname,
        sum(TotalExtents)*64.0/1024                         AS [Total MB],
        sum((TotalExtents - UsedExtents) * 64.0 / 1024.0)   AS [Free MB]

    from #filestats     AS f
        join #filegroup AS g
            on f.filegroup = g.groupid

group by g.groupname


-- Clean up after self
drop table #filestats
drop table #filegroup

set nocount off

Tuesday, April 10, 2012

Synonyms causing editor slowness

So, today we've been tasked with a problem with slowness in the Query editor for one of our users.

She uses the "Design Query in Editor" option of SSMS.  The editor was taking twenty seconds to appear every time she does this on a certain database.  On some of the other databases the Designer was extremely fast.

In researching this we discovered a couple of known bugs.  The first was due to the number of synonyms used in the database.  In this particular case, we have over 2200 synonyms.  It appears, according to Microsoft that this is a known issue with a large number of synonyms.  After some testing, the number of synonyms in a database dramatically affected the speed of the designer.  As the number of synonyms increased the time increased dramatically.


The second issue was that none of the synonyms actually showed up in the Designer.  The designer has four tabs, tables and synonyms being two of them.  The table tabs works just fine, but the synonym tab is empty.  Again, after consulting the Microsoft site; we have found another known issue.

Unfortunately, for us these don't appear to be issues they are planning on resolving anytime soon.


The following items were found on the Microsoft site.

Slowness in Query editor due to synonyms  (known bug.  Won’t be fixed):

Synonyms not showing in Query editor… Synonyms tab  (Known Bug.  Won’t be fixed)





Wednesday, January 18, 2012

SQL Server - Create Alter Table Statements quickly


This is a follow-up to my post for listing all columns with a specific data type.

The goal of the query below is the quickly create all the needed Alter Table statements for the new user defined data type that I'm going to be replacing the old one with.

As stated previously, we are planning on upgrading an older SQL Server 2005 server to SQL Server 2008.  In the process we determined that a couple of the columns would need to have their User Defined Data Types updated.

Instead of manually coding the Alter statements, I spent a few minutes creating a select statement that would allow me to copy the results to another SQL window with all the code needed to make the change.

In the process; I like my code to look pretty, oh so pretty.  So I'm using a couple of replicate statements to pad out spaces for the code to be created.



-- Get the Maximum TableName Length for the specificed type I'm going to update
DECLARE @MaxTableName int
    SET @MaxTableName = (
                   
                            SELECT MAX(LEN(o.Name))

                            FROM sys.columns AS c
                            JOIN sys.types   AS t ON c.user_type_id=t.user_type_id
                            JOIN sys.tables  AS o ON c.Object_id = o.Object_ID
                            Where   t.name = 'Date'
                                and t.is_user_defined = 1
                                and o.Type = 'U'
                        )


-- Get the Maximum Column Name length for the type I'm going to update
DECLARE @maxColumnName int
    SET @MaxColumnName =
                        (
                            SELECT MAX(LEN(c.Name))

                            FROM sys.columns AS c
                            JOIN sys.types   AS t ON c.user_type_id=t.user_type_id
                            JOIN sys.tables  AS o ON c.Object_id = o.Object_ID
                            Where   t.name = 'Date'
                                and t.is_user_defined = 1
                                and o.Type = 'U'
                        )   


-- Now create the pretty Alter statements

-- Will just need to copy the results to a new sql window and
-- you will have all the code needed to fix your problem
            SELECT ( 'ALTER TABLE dbo.['
                            + RTRIM(LTRIM(o.Name))
                            + ']'
                            + REPLICATE (' ',@MaxTableName-LEN(o.Name))
                            + ' ALTER COLUMN ['
                            + RTRIM(LTRIM(c.Name) )
                            + ']'
                            + REPLICATE (' ',@MaxColumnName-LEN(c.Name))
                            + ' DateTime NULL'
                )

            FROM sys.columns AS c
            JOIN sys.types   AS t ON c.user_type_id=t.user_type_id
            JOIN sys.tables  AS o ON c.Object_id = o.Object_ID
            Where   t.name = 'Date'
                and t.is_user_defined = 1
                and o.Type = 'U'
               
            ORDER BY c.OBJECT_ID




So using the above code I got the resulting output:


ALTER TABLE dbo.[Test1]  ALTER COLUMN [Date1]   DateTime NULL
ALTER TABLE dbo.[Test1]  ALTER COLUMN [Date20]  DateTime NULL
ALTER TABLE dbo.[Test11] ALTER COLUMN [Date300] DateTime NULL
ALTER TABLE dbo.[Test11] ALTER COLUMN [Date4]   DateTime NULL
ALTER TABLE dbo.[Test11] ALTER COLUMN [Date50]  DateTime NULL





SQL Server - List all columns with a specific data type

We are in the process of upgrading an old server from SQL Server 2005 to SQL Server 2008.  In the preliminary investigation stage we discovered that there  are a couple User Defined Data Types that are named "Date" and "Time".

As these are now system data types we needed to determine which tables and columns would be affected by the upgrade.

Come to find out, this was a pretty simple task.  Using the system tables; Objects, Columns and Types we were able to pull a list together very quickly of the tables and columns that needed to be updated:


-- Select all Columns which have the user defined type name of 'Date'
SELECT
         SCHEMA_NAME(t.schema_id) AS SchemaName
        ,o.Name                   AS TableName
        ,o.Type_Desc              AS TableDescription
        ,o.Type                   AS TableType
        ,c.Object_id              AS ColumnObjectID 
        ,c.name                   AS ColumnName

        ,c.User_Type_ID
        ,t.name                   AS TypeName
        ,t.is_user_defined
    FROM     sys.columns AS c
        JOIN sys.types   AS t ON c.user_type_id = t.user_type_id
        JOIN sys.objects AS o ON c.Object_id    = o.Object_ID
    Where   t.name            = 'Date'  -- Type Name here
        and t.is_user_defined = 1       -- Yes, we want User Data Types only
        and o.Type            = 'U'     -- User Table Types only
   
ORDER BY o.name,c.name;

   

Well, we had a gotcha here...  The system not only saves which tables and columns that link to the UDT but all Indexes, Statistics, Stored Procedures, functions and views point to the UDT.  To see all affected types just comment out the last AND statement in the above script.

What we ended up having to do was

  • Create new UDT
    • You can not just change a UDT, it has to be created and old one dropped
    • Also means new UDT can not have same name, of course you could get creative here and add a couple of steps to the process so as to retain the same name.
  • Dropping the Statistics
  • Disabling and in some cases having to drop the individual indexes involved
  • Then Altering the table
  • Rebuilding or recreating the indexes
  • Recreate the statistics
  • Recompile the Stored Procedures and UDF's
  • Recreate any affected Views
  • Then you can drop the old UDT

It was strange, even though they still point to the 'date' UDT the functions still worked.


Seems to me, that the the UDT's aren't accomplishing their main objective and that is making it easier to maintain consistency across tables.


For example:

You have a Social Security Number of 11 digits (9 digits plus the two dashes).  You created a UDT for SSN of char(11).  This would allow you to have any tables with a SSN in it to be marked with this UDT and make sure that all are a consistent 11 digits.

Now, the government decides that they need to add a couple more digits to the mess.  So we are tasked with finding and updating all SSN to this new length format.  The UDT makes this very simple to locate all affected columns.

But, updating this column isn't as simple as changing the UDT.  Instead, you have to some major changes through out the database(s).















Thursday, January 12, 2012

Write Function in SQL Server 2005

Something cool I just ran across today.  It appears that in SQL 2005 they added the 'Write' function.

This allows you to update character column, including that with the size of MAX.  This is useful as STUFF didn't work with varchar(max).


-- Create a Temp table to hold our text
CREATE TABLE #tmpTable
    (
        myText NVARCHAR(MAX)
    )


-- Insert sample text we want to mess with
INSERT INTO #tmpTable
Select 'This a very cool Test!'


-- Use the Write function to put the text 'Hello Test'
-- at the beginning of the field
UPDATE #tmpTable
    SET myText.write(
                        'Hello Test'
                        ,0              -- Start index
                        ,0              -- Number of characters to replace
                    )


-- Show that the text has indeed been pushed to the front of the field
select * FROM #tmpTable


-- Now lets use the Write function to delete the first ten characters
UPDATE #tmpTable
    SET myText.Write(
                        ''              -- An Empty String
                       , 0              -- Start at 0
                       , 10             -- The number of characters to replace
                    )

















                   

Tuesday, January 10, 2012

SQL Server Agent System Stored Procedures

A followup post to Sql Server Agent Tables.

In addition to the tables, the are already system stored procedures that will get the information for you.  I've included some of the man arguments.  More information on the stored procedures can be seen at the Microsoft msdn link.

  • sp_help_job
    • if you run this by itself it will show all your jobs
    • Arguments:Or you can Pass in the @job_id or @job_name 
  • sp_help_jobActivity
    • shows the status of the job run.  
    • Arguments:  @job_ID or @job_Name
  •  sp_help_jobHistory
    • shows all the history information for all of the job runs
    • Arguments:  @job_ID or @job_Name
  • sp_help_jobCount
    • Arguments: @schedule_id or @Schedule_name 
    • it will return a count o how many jobs a schedule is tied to.
  • sp_help_jobs_in_schedule
    • Arguments: @schedule_id or @Schedule_name 
    • it will return a a list of all jobs tied to that schedule.sp_help_job_schedule
  • sp_help_jobSchedule
    • Shows jobs that are linked to a schedule
    • Arguments: @job_id or @job_name
  •  sp_help_jobServer
    • Shows information about server tied to a job
    • Arguments: @job_id or @job_name
  • sp_help_jobStep
    • Shows information about steps tied to a job
    • Arguments: @job_id or @job_name
  • sp_help_jobStepLog
    • Shows information about a specific job steplog
    • Arguments: @job_id or @job_name 
    •                  @step_id or @step_name
  • sp_help_schedule
    •  Shows information for schedule
    • Pass in the @schedule_id or @Schedule_name or no parameters for all







Saturday, January 7, 2012

Sql Server Agent Tables

SQL Server stores the SQL Server Agent Jobs in various tables in the msdb.  This provides you an easy way to view jobs, their steps as well as their run history.


  • sysJobs
    • Stores the name, job_ID and related information
  • sysJobSchedules
    • Shows the schedules for the jobs
    • Arguments: Uses Job_ID to link to sysJobs
  • sysJobSteps
    • Shows each step in a job.  Includes the command (actual code used), database and related information
    • Arguments: Uses job_id to link to sysJobs
  •  sysJobHistory
    • Shows the  run history for that job, status, date, run time and duration to complete
    • Arguments: Uses Job_ID and step_ID
  • sysJobServers
    • Stores server related information for jobs
  • sysJobActivity
    • Stores data about the job activity
  • sysJobStepsLogs
    • If it is enabled, it will show specific job step information


Code used to get various information:
use msdb

DECLARE @jobID varchar(50)

-- Get the jobID for a specific job
SET @jobID = ( select job_ID FROM sysJobs where name = 'ETL_Test' )

-- Get the job Information
SELECT * FROM sysJobs     where job_id = @jobID

-- Get the steps for that job
Select * from sysJobSteps where job_id = @jobID order by step_id


-- Show all jobs that use the storedProcedure xs_myTest
SELECT * FROM sysJobSteps where command like '%xs_myTest%'





In addition to the tables there are also System Stored Procedures.  

SQL Server: how to delete a table in chunks

 
I was asked the other day how someone should delete all of rows in a very large table when they couldn't use the TRUNCATE function because there were foreign keys tied to that table.

Deleting all the tables in a single step could cause a large transaction and cause lots of locks.

I found the code below a while back on a better way to minimize the maximum locks and resources on the server.  Deleting in smaller chunks creates a bunch of small transactions that is more manageable for the server to handle.

In addition, if you happen to have an error on one of the chunks, all the deletes up to then are retained and not rolled back, only that one chunk is rolled back.   Then again, if you have an error, all prior deletes are not recoverable.


For this example I'll create a table, populate it with a small set of tables, then the delete command is what we actually use to delete it in chunks.

--- Create table to hold data
CREATE TABLE #Cars (id int)

--- Add a number of Rows to table
DECLARE @x INT
SET @x = 1
WHILE(@x < 10000)
BEGIN
    INSERT INTO #Cars Select round((rand()*100),0)
    SET @x = @x + 1
END

   
--- Delete all the rows in chunks of 5000
--- If the number of ROWS processed in the DELETE statement are not Zero 
--the repeat the delete statement

DeleteThem:
    DELETE TOP(5000) FROM #Cars
if @@RowCount != 0
goto DeleteThem   










Friday, January 6, 2012

SQL Server - Altering tables and renaming columns

 A quick post on how to alter the data types and nullable attributes for a table.

In addition, since you would think that the same command statement would also take care of renaming a column I've added that as well.

First we create a test table to work with, as usual I'll stick with cars.

 After the table has been created, we realized that the Make is only five characters.  That will need to be fixed.

For some reason, it was decided that the Year can not be nullable.  They don't care about the Make or Model, go figure.

Then finally, we are told that Year really shouldn't be used.  They want us to use ModelYear instead.  But wait, ALTER TABLE just doesn't allow column name changes.  So we must use the system stored procedure SP_RENAME.

Below we'll be using SP_RENAME to rename the column.  But it can also be used to rename databases, indexes, objects and user data types.  See: msdn:SP_RENAME for more information.

 
-- Below creates a sample table called cars:

--- Create a Table ---
CREATE TABLE dbo.Cars
(
    ID     INT          Identity(1,1) NOT NULL
  , Year   INT         NULL
  , Make   VarChar(05) NULL
  , Model  VarChar(50) NULL
)

GO

-- the Alter TABLE statement allows you to modify the
-- data type and the nullable attribute of a column.


-- Here we are increasing the size of the Make
-- column to 50 characters from the initially created 05
ALTER TABLE dbo.Cars
    ALTER COLUMN Make Varchar(50) NULL
   
 
GO    

-- Let's prevent null values from being entered
-- into the Year column. 
--Note: If there are already null values in the
--Year column, this command would fail.
ALTER TABLE dbo.Cars
    ALTER COLUMN Year INT NOT NULL

GO

-- There is no ALTER command to rename a column
-- for this you will need to use 'sp_rename'
EXEC sp_rename 'dbo.Cars.Year', 'ModelYear', 'column'


--Note: You will receive a caution message indicating that
--      your change could break any existing scripts or
--      stored procedures.  So make sure you update any
--      affected stored procedures or scripts.



Friday, December 30, 2011

SQL Server - Split a Text String into a table


So, I work for a car insurance company.  I do lots of ETL work.  ETL = Extract Transform and Load.

I regularly receive lists of vehicles.  These lists are not formatted nicely.
 
Generally, all the vehicle information for that vehicle is in one column.  But that doesn't really work to well for me.  I need to separate the Year, Make and Model out into their own columns.

This function has been one of the most useful functions I have run across for this specific task.  I'll pass in the vehicle information as the string input and it will return a table full of rows, one for each item in the vehicle.  It does this based upon the delimiter passed in as the second variable.

I can then use that result set to properly place the information into their appropriate columns in my tables.



-------------------------------------------------
-- Does the function already exist?
-- if so... drop it.
-------------------------------------------------
IF OBJECT_ID(N'dbo.xSplitTextStringIntoTable',N'TF') IS NOT NULL    
BEGIN
    PRINT 'Function Found... Dropping'
    DROP FUNCTION [dbo].[xSplitTextStringIntoTable]
END
GO


--Create the function
CREATE FUNCTION [dbo].[xSplitTextStringIntoTable]
(   
    @Input     NVARCHAR(MAX)
  , @Delimiter NVARCHAR(MAX)
)
RETURNS @Return TABLE
(
        ID    INT IDENTITY(1,1)
    ,   Value NVARCHAR(MAX)
)
AS
---------------------------------------------------------------------------
-- Receives:
--   @Input
--          :The string of values we wish to break up
--   @Delimiter
--          :tells how text is to be split 

--           usually a space or comma, I really like the Pipe 
--           delimiter '|'
--           
--
--    Returned: Table structure with each delimited word returned as a row
--              i.e. '1999 Dodge Viper'
--              returns 3 rows; (1999, Dodge, Viper ) each with its own row
---------------------------------------------------------------------------
BEGIN

    DECLARE @Iterator   INT
    DECLARE @Index      INT
   
    -- We are going to iterate through each item passed in.
    -- An item is based upon the delimiter used
    SET @Iterator = 1
   
    -- Get the index of the first item
    SET @Index = CHARINDEX( @Delimiter, @Input )

    --If there are more than one item it will go through this loop
    --IF NOT it will skip the while loop
    WHILE ( @Index > 0 )

    BEGIN

        -- Insert the item into our table 

        INSERT INTO @Return ( Value )
            SELECT
                Value = LTRIM(RTRIM(
                            SUBSTRING(@Input, 1, @Index - 1)
                              ))

        -- Now remove item in the @Input string since we have
        -- added it to the table
        SET @Input = SUBSTRING(
                                  @Input
                                , @Index + DATALENGTH(@Delimiter) / 2
                                , LEN( @Input )
                               )

        -- increment the iterator by one
        SET @Iterator = @Iterator + 1


        -- Get the next index number, if none found while loop will end
        SET @Index    = CHARINDEX( @Delimiter, @Input )
    END  -- END OF THE WHILE LOOP
   
    -- Here we either
    --     enter the lone item passed in
    --     or the last item passed in
    INSERT INTO @Return (Value)
        SELECT Value = LTRIM(RTRIM( @Input ))

    RETURN
END

GO




-------------------------------------------------
--Now we will check to see if the code works
-------------------------------------------------
DECLARE @Vehicle NVARCHAR(MAX)
SELECT @Vehicle = '1964 Dodge Dakato Station Wagon'


-- Get the full table of values
SELECT *
    FROM   [dbo].[xSplitTextStringIntoTable] (@Vehicle, ' ')


-- Or select from it just like a regular table
-- In this case I just want the value of the 3rd item in the list
SELECT Value
    FROM   [dbo].[xSplitTextStringIntoTable] (@Vehicle, ' ')
    Where ID = 3
   







Thursday, December 29, 2011

SQL Server - Fun With Dates

So, before anyone complains about my method of posting functions.  I like functions.  I work well with them.

But, hey.  Not everyone likes them.  You just want the code.  If you want a somewhat detailed explanation of what is going on, visit their related posts.

So, below is where I'll keep the simple code snippets, with just an intro explanation...

I'll add to this as I add more posts relating to dates or even before I create the related posts.
I'll try and remember to do the same with the various other items I work with.

Oh, and these may not be the fastest queries or the most readable.  But they should work.  If you find one that doesn't let me know.

Enjoy...


-- Set the Date you plan on using

DECLARE @Date DateTime
SET     @Date = '2011-12-14'


-- Get the Last Day of the Month
SELECT DATEADD(d,-DAY(DATEADD(m,1,@Date)),DATEADD(m,1,@date))

-- Get the Last Day of the Prior Month
SELECT DATEADD(d,-Day(@Date),@Date) 


-- Get the First Day of the Month
SELECT DATEADD(d,-Day(@Date) +1 ,@Date)

-- Get the First Day of the prior Month
SELECT DATEADD(d,-DAY(DATEADD(m,-1,@Date)-1 ),DATEADD(m,-1,@date)) 


-- Get the First Day of the next Month
SELECT DATEADD(d,-DAY(DATEADD(m,1,@Date)-1 ),DATEADD(m,1,@date))

--Get the Last Day of the Week
SELECT DATEADD(d,7-(DATEPART(dw,@Date)),@Date)

--Get the First Day of the Given Week
SELECT DATEADD(d,-(DATEPART(dw, @Date) - 1),@Date) 



-- The following will return only the date portion of the 
-- DateTime.  This strips off the Time and leaves only zeros.
SET @Date = GETDATE() --Get current system date and time


SELECT  @Date                            as OriginalDate
      , DATEADD(d,0,DATEDIFF(d,0,@Date)) as StrippedDate