Vous êtes sur la page 1sur 4

1 Nov 01, 2006-1 Nov 01, 2006

SQL SERVER Shrinking Truncate Log File Log Full

Sometime, it looks impossible to shrink the Truncated Log file. Following code always shrinks the
Truncated Log File to minimum size possible.

USE DatabaseName
GO
DBCC SHRINKFILE(<TransactionLogName>, 1)
BACKUP LOG <DatabaseName> WITH TRUNCATE_ONLY
DBCC SHRINKFILE(<TransactionLogName>, 1)
GO

SQL SERVER Fix : Error 14274: Cannot add, update, or delete a job (or its steps or schedules)
that originated from an MSX server. The job was not saved.

To fix the error which occurs after the Windows server name been changed, when trying to update or
delete the jobs previously created in a SQL Server 2000 instance, or attaching msdb database.

Error 14274: Cannot add, update, or delete a job (or its steps or schedules) that originated
from an MSX server. The job was not saved.

Reason:
SQL Server 2000 supports multi-instances, the originating_server field contains the instance name in the
format server\instance. Even for the default instance of the server, the actual server name is used
instead of (local). Therefore, after the Windows server is renamed, these jobs still reference the original
server name and may not be updated or deleted by the process from the new server name. Its a known
problem with SQL2000 SP3.

Fix/Workaround/Solution:

In order to solve the problem you should perform the following steps:

From the Query Analyzer run following steps in order:


SELECT @@servername

and verify if it shows the correct SQL server name.

a) If not, run:
sp_dropserver <'name_returned'>

and then:
sp_addserver <'correct_servername'>, 'local'

to change the SQL server name.

Please restart SQL server service to let the new configuration takes effect.
b) If yes,

Please check the originating_server column in msdb..sysjobs by running:


SELECT *
FROM msdb..sysjobs

and verify if all jobs have the correct server name for originating_server.

If not, update this value with the correct server name by running following script
USE msdb
GO
DECLARE @server sysname
SET @server = CAST(SERVERPROPERTY('ServerName')AS sysname)
UPDATE sysjobs
SET originating_server = @server
WHERE originating_server = '<wrong_servername>'

SQL SERVER Find Stored Procedure Related to Table in Database Search in All
Stored Procedure

Following code will help to find all the Stored Procedures (SP) which are related to one or more specific
tables. sp_help and sp_depends does not always return accurate results.

----Option 1
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%tablename%'
----Option 2
SELECT DISTINCT o.name, o.xtype
FROM syscomments c
INNER JOIN sysobjects o ON c.id=o.id
WHERE c.TEXT LIKE '%tablename%'

SQL SERVER Cursor to Kill All Process in Database

When you run the script please make sure that you run it in different database then the one you want all
the processes to be killed.

CREATE TABLE #TmpWho


(spid INT, ecid INT, status VARCHAR(150), loginame VARCHAR(150),
hostname VARCHAR(150), blk INT, dbname VARCHAR(150), cmd VARCHAR(150))
INSERT INTO #TmpWho
EXEC sp_who
DECLARE @spid INT
DECLARE @tString VARCHAR(15)
DECLARE @getspid CURSOR
SET @getspid = CURSOR FOR
SELECT spid
FROM #TmpWho
WHERE dbname = 'mydb'OPEN @getspid
FETCH NEXT FROM @getspid INTO @spid
WHILE @@FETCH_STATUS = 0
BEGIN
SET @tString = 'KILL ' + CAST(@spid AS VARCHAR(5))
EXEC(@tString)
FETCH NEXT FROM @getspid INTO @spid
END
CLOSE @getspid
DEALLOCATE @getspid
DROP TABLE #TmpWho
GO
SQL SERVER Simple Cursor to Select Tables in Database with Static Prefix and Date Created

Following cursor query runs through database and find all the table with certain prefixed (b_,'delete_). It
also checks if the Table is more than certain days old or created before certain days, it will delete it. We
can have any other opertation on that table like delete, print or reindex.

SET NOCOUNT ON
DECLARE @lcl_name VARCHAR(100)
DECLARE cur_name CURSOR FOR
SELECT name
FROM sysobjects
WHERE type = 'U'
AND crdate <= DATEADD(m,-1,GETDATE())
AND name LIKE 'b_%'
OPEN cur_name
FETCH NEXT FROM cur_name INTO @lcl_name
WHILE @@Fetch_status = 0
BEGIN
SELECT @lcl_name = 'sp_depends ' +@lcl_name
PRINT @lcl_name
-- EXEC (@lcl_name )
FETCH NEXT FROM cur_name INTO @lcl_name
END
CLOSE cur_name
DEALLOCATE cur_name
SET NOCOUNT OFF

SQL SERVER Auto Generate Script to Delete Deprecated Fields in Current Database

I always mark fields to be deprecated with dep_ as prefix. In this way, after few days, when I am sure
that I do not need the field any more I run the query to auto generate the deprecation script. The script
also checks for any constraint in the system and auto generate the script to drop it also.

SELECT 'ALTER TABLE ['+po.name+'] DROP CONSTRAINT [' + so.name + ']'


FROM sysobjects so
INNER JOIN sysconstraints sc ON so.id = sc.constid
INNER JOIN syscolumns col ON sc.colid = col.colid
AND so.parent_obj = col.id AND col.name LIKE 'dep[_]%'
INNER JOIN sysobjects po ON so.parent_obj = po.id
WHERE so.xtype = 'D'
ORDER BY po.name, col.name

SELECT 'ALTER TABLE ['+table_schema+'].['+Table_name+'] DROP COLUMN [' + Column_name + ']'


FROM INFORMATION_SCHEMA.COLUMNS
WHERE column_name LIKE 'dep[_]%'
ORDER BY Table_name, Column_name

SQL SERVER Query to Find ByteSize of All the Tables in Database

SELECT CASE WHEN (GROUPING(sob.name)=1) THEN 'All_Tables'


ELSE ISNULL(sob.name, 'unknown') END AS Table_name,
SUM(sys.length) AS Byte_Length
FROM sysobjects sob, syscolumns sys
WHERE sob.xtype='u' AND sys.id=sob.id
GROUP BY sob.name
WITH CUBE

SQL SERVER Query to Display Foreign Key Relationships and Name of the Constraint for Each
Table in Database
UPDATE : SQL SERVER 2005 Find Tables With Foreign Key Constraint in Database

This is very long query. Optionally, we can limit the query to return results for one or more than one
table.

SELECT
K_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON C.CONSTRAINT_NAME =
FK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON C.UNIQUE_CONSTRAINT_NAME =
PK.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME =
CU.CONSTRAINT_NAME
INNER JOIN (
SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON i1.CONSTRAINT_NAME =
i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT ON PT.TABLE_NAME = PK.TABLE_NAME
---- optional:
ORDER BY
1,2,3,4
WHERE PK.TABLE_NAME='something'WHERE FK.TABLE_NAME='something'
WHERE PK.TABLE_NAME IN ('one_thing', 'another')
WHERE FK.TABLE_NAME IN ('one_thing', 'another')