SqlServer : Saving changes is not permitted

With SqlServer, when you want to change the structure of a table, you may encounter the following warning message :

“Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can’t be re-created or enabled the option Prevent saving changes that require the table to be re-created.”

warn1

To avoid this warning message, go to the “tools” menu, then click “Options”

toolsoptionsClick on the “Designer” node

Designers

Then uncheck the “Prevent saving changes that require table re-creation” checkbox

uncheck
And you are done, the message box will not appears anymore

Incoming search terms:

  • sqlserver saving changes is not permitted

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 a new query, type


EXEC sp_depends @objname='tableName'

(replace “tableName” by the name of your table)

then execute.

result example :

example result

example result

Incoming search terms:

  • how can i get table in stored procedure
  • to get list of stored procedures using specific table

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 EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''''?'''') AND type in (N''''U''''))
 BEGIN
 IF columnproperty(object_id(''''?''''), ''''CreatedDate'''', ''''ColumnId'''') is null
 BEGIN
 ALTER TABLE ? ADD [CreatedDate] datetime NOT NULL
 END
 END
 END
'')

' ;
print ' [CreatedDate] Column is created for each user table'

Example 2 :

Add the “CreatedUserID” column to all tables and enable constraints. For the example, the reference use the aspnet_Users table

print 'Add [CreatedUserID] 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 EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''''?'''') AND type in (N''''U''''))
 BEGIN
 IF columnproperty(object_id(''''?''''), ''''CreatedUserID'''', ''''ColumnId'''') is null
 BEGIN
 ALTER TABLE ? ADD [CreatedUserID] uniqueidentifier NOT NULL
 END
 END
 END
'')
-- add constraint

 declare @constraintName as nvarchar(max)
 set @constraintName = ''FK_?_CreatedUserID''
 IF (CHARINDEX(''aspnet_'',@constraintName) = 0) -- no aspnet membership
 BEGIN
 set @constraintName = REPLACE(@constraintName,''[dbo].['','''')
 set @constraintName = REPLACE(@constraintName,'']'','''')
 IF NOT EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N''''+@constraintName+'''') AND type = (N''F''))
 BEGIN
 exec(''
 ALTER TABLE ? ADD CONSTRAINT [''+@constraintName+''] FOREIGN KEY([CreatedUserID])
 REFERENCES [aspnet_Users]([UserID])
 '')
 END
 END

' ;
print ' [CreatedUserID] Column is created for each user table'

The difficulty is to add the right numbers of  ‘ .

Incoming search terms:

  • sp_msforeachtable

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 you want to remove the leading zeros

DECLARE @iVal nvarchar(max)
set @iVal = '0000000120345FER'

DECLARE @iVal2 nvarchar(max)
set @iVal2= SUBSTRING(@iVal, PATINDEX('%[^0]%', @iVal+'.'), LEN(@iVal))

print @iVal2

result is “120345FER”