Altering Multiple Tables at once - tsql

I'm trying to alter multiple SQL Server 2008 R2 tables at one time.
This is my code:
use DatabaseName
go
Declare #SchemaUsed varchar(20) = 'dbo'
create table #Tables
(
TableName varchar(100), Processed int
)
insert into #Tables
select top 1 table_name, 0
from INFORMATION_SCHEMA.TABLES
where TABLE_SCHEMA = #SchemaUsed
and table_type = 'Base Table'
and (TABLE_NAME like 'PM%' )
ORDER BY TABLE_NAME
DECLARE #TableName varchar(max)
DECLARE #SQL varchar(max)
WHILE EXISTS (select top 1 'x' from #Tables where Processed = 0)
BEGIN
SET #TableName = (select top 1 TableName from #Tables where Processed = 0)
Set #SQL = 'ALTER TABLE ' + #SchemaUsed + '.' + #TableName + ' ADD [identityID] bigint IDENTITY(1, 1) NOT NULL '
-- Set #SQL = '''' + #SQL + ''''
Print #SQL
EXEC #SQL;
update #Tables
set Processed = 1
where TableName = #TableName
END
drop table #Tables
I can't get this to work to save my life and get the following error:
Lookup Error - SQL Server Database Error: The name 'ALTER TABLE
dbo.PM1GTVLV ADD [identityID] bigint IDENTITY(1, 1) NOT NULL ' is not
a valid identifier.
I've also tried multiple string variations and using sp_executesql as well.
Can someone point out where I've gone wrong?

Try
DECLARE #SQL NVARCHAR(MAX);
EXEC sp_executesql #SQL;
Instead of EXEC #sql.
As an aside, this is a much more usable version of the same code IMHO:
DECLARE #SchemaUsed VARCHAR(20) = 'dbo';
DECLARE #sql NVARCHAR(MAX) = N'';
SELECT #sql += CHAR(13) + CHAR(10) + N'ALTER TABLE '
+ QUOTENAME(#SchemaUsed) + '.'
+ QUOTENAME(name) + ' ADD [identityID]
BIGINT IDENTITY(1,1) NOT NULL;'
FROM sys.tables
WHERE SCHEMA_NAME([schema_id]) = #SchemaUsed
AND name LIKE 'PM%';
PRINT #sql;
--EXEC sp_executesql #sql;
Or even better:
DECLARE #SchemaUsed VARCHAR(20) = 'dbo';
DECLARE #sql NVARCHAR(MAX) = N'';
SELECT #sql += CHAR(13) + CHAR(10) + N'ALTER TABLE '
+ QUOTENAME(#SchemaUsed) + '.'
+ QUOTENAME(name) + ' ADD [identityID]
BIGINT IDENTITY(1,1) NOT NULL;'
FROM sys.tables AS t
WHERE SCHEMA_NAME([schema_id]) = #SchemaUsed
AND name LIKE 'PM%'
AND NOT EXISTS (SELECT 1 FROM sys.columns AS c
WHERE [object_id] = t.[object_id]
AND c.is_identity = 1);
PRINT #sql;
--EXEC sp_executesql #sql;

To execute a character string, EXEC requires parenthesis around the string (or character variable) as shown in the BOL syntax:
EXEC (#SQL);

Related

sp_MSForeachdb inserting into declared virtual table issue

I am having issues getting information to insert into the #TBL2 Table.
what am i doing wrong?
DECLARE #command varchar(1000)
DECLARE #SQLStatment varchar(1000)
DECLARE #TBL2 table (
Database_Name nvarchar(max),
SI_SITE nvarchar(max),
SI_DB_USER nvarchar(max)
)
SELECT #command = 'IF ''?'' NOT IN(''master'', ''model'', ''msdb'', ''tempdb'') BEGIN USE ? insert into #tbl2 EXEC('+ #SQLStatment +') END'
set #SQLStatment = 'select top 1 Db_Name() as Database_Name, SI_SITE, SI_DB_USER from t_site'
EXEC master.. sp_MSForeachdb #command
select * from #TBL2
Try This:
There are several issues with the approach you are taking:
I don't think the "USE" statement can be dynamic (could be wrong)
The SQL is declared after you are trying to use it.
sp_msforeachdb is undocumented and shouldn't be relied on, even though it can work in many circumstances.
My approach uses the sys.databases and string concatenation to generate the appropriate SQL string to get the data you want from each table in all databases except the system databases, then executes the results into a temp table. The approach also assumes dbo schema. Adjust if necessary.
declare #SQL nvarchar(max)
set #SQL = ''
Create Table #TBL2 (
Database_Name nvarchar(max),
SI_SITE nvarchar(max),
SI_DB_USER nvarchar(max)
)
Select #SQL = #SQL + 'INSERT INTO #TBL2 (Database_Name, SI_SITE, SI_DB_USER) select top 1 ''' + name + ''' as Database_Name, SI_SITE, SI_DB_USER from ' + name + '..t_site;' + char(13)
From sys.databases
Where name not in ('master', 'model', 'msdb', 'tempdb')
print #SQL
exec sp_executesql #SQL
Select * From #TBL2
drop table #TBL2
I still have some issues to work out but this is what it may look like
declare #SQL nvarchar(max)
set #SQL = ''
Create Table #TBL3 (
Server_Name nvarchar(max),
Database_Name nvarchar(max),
DB_Owner nvarchar(max),
SI_SITE nvarchar(max),
SI_DB_USER nvarchar(max),
DB_Creation_date nvarchar(max),
DB_state nvarchar(max),
DB_SQL_version nvarchar(max)
)
Select #SQL = #SQL + 'INSERT INTO #TBL3
(
Server_Name
,[Database_Name]
,[DB_Owner]
,[DB_Creation_Date]
,[DB_State]
,[DB_SQL_Version]
,[SI_SITE]
,[SI_DB_USER]
)
SELECT
Server_Name
,quotename(''' + name + ''')
,[DB_Owner]
,[DB_Creation_date]
,[DB_state]
,[DB_SQL_version]
,[SI_SITE]
,[SI_DB_USER]
From( SELECT TOP 1
[SI_SITE]
,[SI_DB_USER]
From [' + name + ']..[t_site]) Q1,
( SELECT
##SERVERNAME as [Server_Name]
,suser_sname(owner_sid) as [DB_Owner]
,[Create_Date] as [DB_Creation_date]
,[state_desc] as [DB_state]
,case [compatibility_level]
when 80 then ''SQL Server 2000'' when 90 then ''SQL Server 2005'' when 100 then ''SQL Server 2008'' when 110 then ''SQL Server 2012'' when 120 then ''SQL Server 2014'' when 130 then ''SQL Server 2016'' when 140 then ''SQL Server 2017'' else cast(compatibility_level as varchar(100)) end as DB_SQL_version
from [sys].[databases]
where [name] = ''' + name + '''
) Q2;' + char(13)
From sys.databases
Where name not in ('master', 'model', 'msdb', 'tempdb')
and name not in ('DBX') -- where [T_site] table does not exist
and name not in ('DBY') -- offline, need offline clause added
print #SQL
exec sp_executesql #SQL
Select * From #TBL3
DROP TABLE #TBL3

Using a Cursor to Create Triggers for all tables in a database

I am using SQL Server 2005
I am trying to create a trigger for each table contained within a database and keep getting an error which I do not understand and can not fix
If anyone has an idea of why I am getting this error or how I can resolve it that would be brilliant.
DECLARE #TableName AS VARCHAR(100)
DECLARE #audit_action as varchar(100)
DECLARE RDS_cusor CURSOR FOR
SELECT TABLE_NAME FROM [RawDataStorage].INFORMATION_SCHEMA.Tables WHERE TABLE_NAME LIKE 't_%' AND TABLE_NAME NOT LIKE 'tbl_%'
OPEN RDS_cursor
FETCH NEXT FROM RDS_cursor INTO #TableName
WHILE ##FETCH_STATUS = 0
BEGIN
CREATE TRIGGER dbo.After_Insert_Trig
ON [dbo].[t_Agent]
AFTER INSERT,DELETE,UPDATE
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO RawDataStorage_Audit
(TableName,Audit_Action,Audit_Timestamp)
VALUES('TableName','audit_action',getdate());
END
GO
FETCH NEXT FROM RDS_cursor INTO #TableName
END
CLOSE RDS_cursor
DEALLOCATE RDS_cursor
This is the error I am getting:
Msg 156, Level 15, State 1, Line 15
Incorrect syntax near the keyword 'TRIGGER'.
Msg 102, Level 15, State 1, Line 26
Incorrect syntax near 'END'.
Msg 137, Level 15, State 2, Line 2
Must declare the scalar variable "#TableName".
any help would be greatly appreachiated
I managed to put this together, its a dynamic query that builds the sql to create the triggers using a cursor to grab the table names from the sysobjects.
CREATE TABLE [dbo].[RawDataStorage_Audit](
ID INT IDENTITY(1,1) NOT NULL,
TableName VARCHAR(50) NULL,
audDateLastChanged DATETIME null
)
Declare #TableName nvarchar(100)
Declare #SQL nvarchar(1000)
declare tables_cursor CURSOR FOR
select [name] from sysobjects
where xtype = 'U'
and [name] not like 'sys%'
order by [name]
Open tables_cursor
fetch next from tables_cursor into #TableName
WHILE ##FETCH_STATUS = 0
Begin
--create trigger for insert on each table
print #SQL
set #SQL = 'create trigger trg_u_'+#TableName+'_Inserted '
set #SQL = #SQL + ' on '+#TableName
set #SQL = #SQL + ' for insert '
set #SQL = #SQL + ' as '
set #SQL = #SQL + ' begin '
set #SQL = #SQL + ' insert into [dbo].[RawDataStorage_Audit] '
set #SQL = #SQL + ' select ''' + #TableName +''', getdate()'
set #SQL = #SQL + ' from inserted'
set #SQL = #SQL + ' end'
exec (#SQL)
print #SQL
fetch next from tables_cursor into #TableName
end
CLOSE tables_cursor
DEALLOCATE tables_cursor

How to create view for all tables in database?

I want to make views from all tables already exist in database that’s hard task to catch the tables one by one and make create view XXXX as select * from Table_name .I find something that it is possible with cursor and the code is :
DECLARE #TableName sysname
DECLARE #ColumnCount INT
DECLARE #ColumnID INT
DECLARE #SelectColumn NVARCHAR(500)
DECLARE #sql NVARCHAR(max) = ''
DECLARE QUERYINFO CURSOR FOR
SELECT
t.name AS TableName,
ccount.ColumnCount,
c.column_id AS ColumnID,
CASE WHEN c.column_id <> ccount.ColumnCount
THEN c.name + ', '
ELSE c.name
END AS SelectColumn
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id=c.object_id
INNER JOIN (
SELECT object_id,COUNT(*) AS ColumnCount
FROM sys.columns
GROUP BY object_id
) ccount ON t.object_id = ccount.object_id
ORDER BY t.Name,c.column_id
OPEN QUERYINFO
FETCH NEXT FROM QUERYINFO INTO #TableName,#ColumnCount,#ColumnID,#SelectColumn
WHILE ##FETCH_STATUS = 0
BEGIN
IF #ColumnID = 1
BEGIN
SET #sql = 'CREATE VIEW v_' + #TableName + ' AS SELECT ' + #SelectColumn
END
ELSE
BEGIN
SET #sql = #sql + #SelectColumn
END
IF #ColumnID = #ColumnCount
BEGIN
SET #sql = #sql + ' FROM ' + #TableName
EXEC sys.sp_executesql #sql
SET #sql = ''
END
FETCH NEXT FROM QUERYINFO INTO #TableName,#ColumnCount,#ColumnID,#SelectColumn
END
CLOSE QUERYINFO
DEALLOCATE QUERYINFO
but it has error and I don't know how solve it
This is what I would do with dynamic SQL; checking first to see if the view exists, then creating it with all of the columns (not by using SELECT * FROM...).
DECLARE #SQL nvarchar(MAX)
SET #SQL = N''
SELECT
#SQL = #SQL +
N'IF EXISTS(SELECT 1 FROM sys.objects WHERE name = N''v_' + t.name + N''' AND type = N''V'') BEGIN DROP VIEW [v_' + t.name + '] END CREATE VIEW [v_' + t.name + N'] AS SELECT ' +
STUFF(
(SELECT N',' + c.name
FROM
sys.columns AS c
WHERE
c.OBJECT_ID = t.OBJECT_ID
ORDER BY
column_id
FOR XML PATH(''), TYPE).value('.',N'nvarchar(max)')
,1,1,N'')
+ N' FROM [' + t.name + N'];'
FROM
sys.tables AS t
EXEC sp_executesql #SQL

Adding GUID parameter to a sql string

I am having complications adding a guid type to a sql string and getting the query to go. I have to write the sql this because I need to pass the name database as a parameter.
The following code is what I'm trying to do but it doesnt work. No matter what I do it doesn't read the GUID correctly. It gives me the following error when executing the stored proc. Probably because guid has "-" in them
Incorrect syntax near '-'.
Here's the code:
ALTER PROCEDURE [dbo].[templatesAndBaskets_Select]
#guid uniqueidentifier,
#DatabaseName varchar(50)
AS
BEGIN
Declare #sql varchar(max)
Set #sql=
'select soldtoid
from ' + #DatabaseName + '.templatesAndBaskets' +
' where ordergroupid = ' + CONVERT(varchar(max),#guid)
print #sql
EXECUTE SP_EXECUTESQL #sql
END
You probably just simply need to put single quotes around the GUID :
SET #sql =
N'SELECT soldtoid FROM ' + #DatabaseName + N'.templatesAndBaskets' +
N' WHERE ordergroupid = ''' + CONVERT(nvarchar(max), #guid) + N''''
That should result in a SQL statement something like this:
SELECT soldtoid FROM database.templatesAndBaskets
WHERE ordergroupid = '5E736CE7-5527-40ED-8499-2CA93FC7BC9C'
which is valid - yours without the single quotes around the GUID isn't valid...
Update: you also need to change your #sql variable to NVARCHAR for sp_Executesql - try this:
ALTER PROCEDURE [dbo].[templatesAndBaskets_Select]
#guid uniqueidentifier,
#DatabaseName NVARCHAR(50)
AS
BEGIN
DECLARE #sql NVARCHAR(MAX)
SET #sql =
N'SELECT soldtoid FROM ' + #DatabaseName + N'.templatesAndBaskets' +
N' WHERE ordergroupid = ''' + CONVERT(nvarchar(max), #guid) + N''''
PRINT #sql
EXECUTE sp_ExecuteSQL #sql
END
Does that work??
Put the #guid between '
ALTER PROCEDURE [dbo].[templatesAndBaskets_Select]
#guid uniqueidentifier,
#DatabaseName varchar(50)
AS
BEGIN
Declare #sql varchar(max)
Set #sql=
"select soldtoid from " + #DatabaseName + ".templatesAndBaskets" +
" where ordergroupid = '" + CONVERT(varchar(max),#guid)+"'"
print #sql
EXECUTE SP_EXECUTESQL #sql
END

Trying to use a Case statement inside a cursor

I am trying to make the following work:
declare #ActTable as varchar(1000)
declare #cPK as VarChar(100)
declare #SQL as nvarchar(2000)
declare #FK as VarChar(100)
declare #FKRef as VarChar(200)
declare #TblRef as varchar (100)
create table #temp (
M2MTable varchar(50),
PK varchar (100),
FK Varchar(100),
FKRefTable Varchar(50))
insert into #temp
select 'slcdpm' , 'fcustno', '','' union all
select 'somast' , 'fsono', 'fcustno','slcdpm' union all
select 'soitem' , 'fsono,finumber', 'fsono','somast' union all
select 'sorels', 'fsono,finumber,frelease', 'fsono,finumber','soitem' union all
select 'qtmast', 'fquoteno', 'fcustno', 'slcdpm' union all
select 'qtitem' , 'fquoteno', 'fquoteno','qtmast' union all
select 'armast', 'fcinvoice','fcustno','scldpm' union all
select 'aritem','fcinvoice,fitem','fcinvoice','armast' union all
select 'apvend', 'fvendno','','' union all
select 'apmast','fvendno,fcinvoice','fvendno','apvend'union all
--select 'apitem','fvendno,fcinvoice,union all
select 'pomast','fpono','fvendno','apvend'union all
select 'poitem', 'fpono,fitemno','fpono','pomast' union all
select 'shmast', 'fshipno','fsokey','sorels' union all
select 'shitem','fshipno,fitemno','fshipno','shmast' -- union all
declare M2M_AddFK cursor for select M2MTable,FK,FKRefTable from #temp
open M2M_AddFK
fetch next from M2M_AddFK into #ActTable,#FK,#TblRef
while ##FETCH_STATUS = 0
Begin
case
when #FK <> ''then Set #SQL = N'alter table dbo.'+ #ActTable + ' ADD FOREIGN KEY (' + #FK + ') References DBO.' + #TblRef + '(' + #FK + ')'
Print #SQL
fetch next from M2M_AddFK into #ActTable,#FK,#TblRef
else
fetch next from M2M_AddFK into #ActTable,#FK,#TblRef
end
end
close M2M_AddFK
deallocate M2M_AddFK
drop table #temp
Please notice the Case statement:
case
when #FK <> ''then Set #SQL = N'alter table dbo.'+ #ActTable + ' ADD FOREIGN KEY (' + #FK + ') References DBO.' + #TblRef + '(' + #FK + ')'
Print #SQL
fetch next from M2M_AddFK into #ActTable,#FK,#TblRef
else
fetch next from M2M_AddFK into #ActTable,#FK,#TblRef
end
I simply want to create the alter table statement when there is a #FK value and to skip it if it's ''.
Can someone point me in the right direction?
You have to uase an IF ELSE statement.
Also, Formatting the code a little, will make it a LOT easier to read X-)
Replace the WHILE loop with
while ##FETCH_STATUS = 0
Begin
IF #FK <> ''
BEGIN
Set #SQL = N'alter table dbo.'+ #ActTable + ' ADD FOREIGN KEY (' + #FK + ') References DBO.' + #TblRef + '(' + #FK + ')'
Print #SQL
fetch next from M2M_AddFK into #ActTable,#FK,#TblRef
END
ELSE
BEGIN
fetch next from M2M_AddFK into #ActTable,#FK,#TblRef
end
end
So that your entire statement looks like
declare #ActTable as varchar(1000)
declare #cPK as VarChar(100)
declare #SQL as nvarchar(2000)
declare #FK as VarChar(100)
declare #FKRef as VarChar(200)
declare #TblRef as varchar (100)
create table #temp (
M2MTable varchar(50),
PK varchar (100),
FK Varchar(100),
FKRefTable Varchar(50))
insert into #temp
select 'slcdpm' , 'fcustno', '','' union all
select 'somast' , 'fsono', 'fcustno','slcdpm' union all
select 'soitem' , 'fsono,finumber', 'fsono','somast' union all
select 'sorels', 'fsono,finumber,frelease', 'fsono,finumber','soitem' union all
select 'qtmast', 'fquoteno', 'fcustno', 'slcdpm' union all
select 'qtitem' , 'fquoteno', 'fquoteno','qtmast' union all
select 'armast', 'fcinvoice','fcustno','scldpm' union all
select 'aritem','fcinvoice,fitem','fcinvoice','armast' union all
select 'apvend', 'fvendno','','' union all
select 'apmast','fvendno,fcinvoice','fvendno','apvend'union all
--select 'apitem','fvendno,fcinvoice,union all
select 'pomast','fpono','fvendno','apvend'union all
select 'poitem', 'fpono,fitemno','fpono','pomast' union all
select 'shmast', 'fshipno','fsokey','sorels' union all
select 'shitem','fshipno,fitemno','fshipno','shmast' -- union all
declare M2M_AddFK cursor for select M2MTable,FK,FKRefTable from #temp
open M2M_AddFK
fetch next from M2M_AddFK into #ActTable,#FK,#TblRef
while ##FETCH_STATUS = 0
Begin
IF #FK <> ''
BEGIN
Set #SQL = N'alter table dbo.'+ #ActTable + ' ADD FOREIGN KEY (' + #FK + ') References DBO.' + #TblRef + '(' + #FK + ')'
Print #SQL
fetch next from M2M_AddFK into #ActTable,#FK,#TblRef
END
ELSE
BEGIN
fetch next from M2M_AddFK into #ActTable,#FK,#TblRef
end
end
close M2M_AddFK
deallocate M2M_AddFK
drop table #temp