Archive for the ‘Sql Server’ Category

How to get the list of all stored procedures using a specific table

Big projects with a lot of stored procedures can be a mess to maintain.
If you change one field in a table, you have to check if there is no side effect in the stored procedures using this table.
This “sp_depends” system stored procedure can help you finding your stored procedures using the table you modified.
Just create [...]

More »

How to use the undocumented stored procedure sp_msforeachtable to add a column to all tables in a database ?

There are two examples, in both I add a column to each tables except the spnet membership tables.
Example :
Add the “CreatedDate” column to all tables :

print ‘Add [CreatedDate] Column for each user table’

EXEC sp_msforeachtable

– add column
exec(”
declare @tableName as nvarchar(max)
set @tableName = ””_?_””

IF (CHARINDEX(””aspnet_””,@tableName) = 0) — no aspnet membership
BEGIN
IF [...]

More »

Zeros and strings manipulation in Tsql

How do I convert an int to a zero padded string in T-SQL?
Let’s say I have an int with the value of 1.
How can I convert that int to a zero padded string, such as “00000001″?

DECLARE @iVal int
SET @iVal = 1
select REPLACE(STR(@iVal,8,0),’ ‘,’0′)

result is “00000001″
And the inverse :
Let’s say you have a string “0000000120345FER” and [...]

More »