Hi I have developed a stored procedure in which I declared the a table variable called #TEMP_TABLE but for this line
SET #SQL1 = 'UPDATE PO_STAGE set PO_STAGE.ISVALID = ''N''
FROM ERP_ADMIN.'+#STAGE_TABLE+' PO_STAGE, #TEMP_TABLE TMP
where PO_STAGE.ERP_PO_ID = TMP.ERP_POID'
exec (#SQL1)
I am getting "Must declare the table variable "#TEMP_TABLE"." error.
Please help me in this regards.Below is the code for Stored Procedure.
CREATE PROCEDURE [ERP_ADMIN].[VALIDATE_PURCHASE_ORDER]
#STAGE_TABLE nvarchar(50)
-- #errorProperty nvarchar(50) output,
-- #errorValue nvarchar(120) output
AS
BEGIN
SET NOCOUNT ON;
DECLARE #rule INTEGER
DECLARE #approvalcheckingrecord VARCHAR(200)
DECLARE #APP_VALIDFROMDATE DATETIME,
#APP_VALIDTODATE DATETIME,
#numUpdated INTEGER,
#numInserted INTEGER,
#systodate VARCHAR(160),
#SQL1 [nvarchar](1200),
#SQL2 [nvarchar](1200),
#SOUCOL [nvarchar](200)
-- SELECT #systodate = CONVERT(VARCHAR(23), ERP_ADMIN.GETCURRENTDATE(), 110) +' '+ CONVERT(VARCHAR(23), ERP_ADMIN.GETCURRENTDATE(), 108)
--SELECT #systodate = CONVERT(VARCHAR(23), GETDATE(), 110) +' '+ CONVERT(VARCHAR(23), GETDATE(), 108)
SET #SQL1 = ''
SET #SQL1 = 'SELECT ERP_SOURCE_COLUMN
FROM [ERP_ADMIN].[ERP_STAGE_PROP_MAPPING] where [ERP_SOURCE_TABLE] = '''+#STAGE_TABLE+''' and [ERP_REPORT_COLOMNS] = ''Y'''
-- PRINT #SQL1
SET #SQL2 = 'DECLARE rpt_cursor CURSOR GLOBAL FOR ' + #SQL1
exec (#SQL2)
DECLARE #SOURCE_COLOMN nvarchar(80)
SET #SOUCOL = ''
OPEN rpt_cursor
WHILE (0 = 0)
--begin of while
BEGIN
FETCH NEXT FROM rpt_cursor INTO #SOURCE_COLOMN
IF (##FETCH_STATUS = -1)
BREAK
IF (#SOUCOL = '')
SET #SOUCOL = 'convert(nvarchar(255),ISNULL('+#SOURCE_COLOMN+',''''))+'',''+'
ELSE
SET #SOUCOL = #SOUCOL + 'convert(nvarchar(255),ISNULL('+#SOURCE_COLOMN+',''''))+'',''+'
END
CLOSE rpt_cursor
DEALLOCATE rpt_cursor
PRINT #SOUCOL
DECLARE #TEMP_TABLE TABLE (
ERP_POID nvarchar(50),
ERRORCD nvarchar(50),
ERRORMSG nvarchar(120)
)
SET #SQL1 = ''
SET #SQL1 = 'SELECT ERP_SOURCE_COLUMN
FROM [ERP_ADMIN].[ERP_STAGE_PROP_MAPPING] where [ERP_SOURCE_TABLE] = '''+#STAGE_TABLE+''' and [ERP_REPORT_COLOMNS] = ''Y'''
SET #SQL1 = 'SELECT [ERP_SOURCE_TABLE]
,[ERP_SOURCE_COLUMN]
,[ERP_MANDATORY_CHECK]
,[ERP_DATA_CHECK]
,[ERP_ERROR_PROPERTY1]
,[ERP_ERROR_VALUE1]
,[ERP_ERROR_PROPERTY2]
,[ERP_ERROR_VALUE2]
FROM [ERP_ADMIN].[ERP_STAGE_PROP_MAPPING] where [ERP_SOURCE_TABLE] = '''+#STAGE_TABLE+''' and ([ERP_MANDATORY_CHECK] IN (''K'',''Y'') or [ERP_DATA_CHECK] IN (''D'',''N''))'
PRINT #SQL1
SET #SQL2 = 'DECLARE stage_cursor CURSOR GLOBAL FOR ' + #SQL1
/** DECLARE stage_cursor CURSOR LOCAL FOR
/****** Script for SelectTopNRows command from SSMS ******/
SELECT [ERP_SOURCE_TABLE]
,[ERP_SOURCE_COLUMN]
,[ERP_MANDATORY_CHECK]
,[ERP_DATA_CHECK]
,[ERP_ERROR_PROPERTY1]
,[ERP_ERROR_VALUE1]
,[ERP_ERROR_PROPERTY2]
,[ERP_ERROR_VALUE2]
FROM [ERP_ADMIN].[ERP_STAGE_PROP_MAPPING] where [ERP_SOURCE_TABLE] = '+#STAGE_TABLE+' and ([ERP_MANDATORY_CHECK] IN ('K','Y') or [ERP_DATA_CHECK] IN ('D','N')) **/
exec(#SQL2)
DECLARE #CUR_SOURCE_TABLE [nvarchar](120),
#CUR_SOURCE_COLUMN [nvarchar](120),
#CUR_MANDATORY_CHECK [nvarchar](20),
#CUR_DATA_CHECK [nvarchar](20),
#CUR_ERROR_PROPERTY1 [nvarchar](50),
#CUR_ERROR_VALUE1 [nvarchar](120),
#CUR_ERROR_PROPERTY2 [nvarchar](50),
#CUR_ERROR_VALUE2 [nvarchar](120)
OPEN stage_cursor
WHILE (0 = 0)
--begin of while
BEGIN
FETCH NEXT FROM stage_cursor INTO
#CUR_SOURCE_TABLE,
#CUR_SOURCE_COLUMN,
#CUR_MANDATORY_CHECK,
#CUR_DATA_CHECK,
#CUR_ERROR_PROPERTY1,
#CUR_ERROR_VALUE1,
#CUR_ERROR_PROPERTY2,
#CUR_ERROR_VALUE2
IF (##FETCH_STATUS = -1)
BREAK
SET #SQL1 = ''
SET #SQL2 = ''
PRINT #CUR_SOURCE_COLUMN
-- IF(#CUR_SOURCE_COLUMN = 'ERP_PO_DOCNUMBER')
IF (#CUR_MANDATORY_CHECK = 'Y' OR #CUR_MANDATORY_CHECK = 'K')
BEGIN
-- SET #SQL1 = 'SELECT ''aaa'' as column1, ERP_PO_DOCNUMBER as column2
-- from PURCHASE_ORDER_STAGE where '+ #CUR_SOURCE_COLUMN +' IS NULL'
SET #SQL1 = 'SELECT ERP_PO_ID, '''+#CUR_ERROR_PROPERTY1+''' as column1,'+#SOUCOL+'''|1| ERR_01 '''''+#CUR_ERROR_VALUE1+'''+'' or null in input Worksheet'' as column2
from [ERP_ADMIN].'+#STAGE_TABLE+' where '+ #CUR_SOURCE_COLUMN +' IS NULL'
PRINT #SQL1
INSERT INTO #TEMP_TABLE
exec (#SQL1)
END
IF (LTRIM(RTRIM(#CUR_DATA_CHECK)) = 'D')
BEGIN
-- SET #SQL1 = 'SELECT ''aaa'' as column1, ERP_PO_DOCNUMBER as column2
-- from PURCHASE_ORDER_STAGE where '+ #CUR_SOURCE_COLUMN +' IS NULL'
SET #SQL1 = 'SELECT ERP_PO_ID, '''+#CUR_ERROR_PROPERTY2+''' as column1,'+#SOUCOL+'''|1| ERR_01 '''''+#CUR_ERROR_VALUE2+'''+'' in the input Worksheet'' as column2
from [ERP_ADMIN].'+#STAGE_TABLE+' where isdate('+ #CUR_SOURCE_COLUMN +') =0'
PRINT #SQL1
INSERT INTO #TEMP_TABLE
exec (#SQL1)
END
IF (LTRIM(RTRIM(#CUR_DATA_CHECK)) = 'N')
BEGIN
PRINT #CUR_SOURCE_COLUMN
SET #SQL1 = 'SELECT ERP_PO_ID, '''+#CUR_ERROR_PROPERTY2+''' as column1,'+#SOUCOL+'''|1| ERR_01 '''''+#CUR_ERROR_VALUE2+'''+'' in the input Worksheet'' as column2
from [ERP_ADMIN].'+#STAGE_TABLE+' where isnumeric('+ #CUR_SOURCE_COLUMN +') =0'
PRINT #SQL1
INSERT INTO #TEMP_TABLE
exec (#SQL1)
END
END
CLOSE stage_cursor
DEALLOCATE stage_cursor
SET #SQL1 = ''
SET #SQL1 = 'UPDATE PO_STAGE set PO_STAGE.ISVALID = ''N'' FROM ERP_ADMIN.'+#STAGE_TABLE+' PO_STAGE, #TEMP_TABLE TMP where PO_STAGE.ERP_PO_ID = TMP.ERP_POID'
PRINT #SQL1
exec (#SQL1)
/** DECLARE update_stage_cursor CURSOR LOCAL FOR
SELECT ERP_POID FROM #TEMP_TABLE
DECLARE #CURPOID [nvarchar](50)
OPEN update_stage_cursor
WHILE (0 = 0)
--begin of while
BEGIN
FETCH NEXT FROM update_stage_cursor INTO
#CURPOID
IF (##FETCH_STATUS = -1)
BREAK
END
CLOSE update_stage_cursor
DEALLOCATE update_stage_cursor **/
-- SELECT A.ERP_PO_ID FROM ERP_ADMIN.PURCHASE_ORDER_STAGE A, #TEMP_TABLE B where ERP_PO_ID = B.ERP_POID
SELECT ERRORCD, ERRORMSG FROM #TEMP_TABLE
END
GO
You should pass #TEMP_TABLE as parameter. To pass Table parameter you should create table type at first:
IF NOT EXISTS (SELECT 1 FROM sys.types WHERE NAME = 'TEMP_TABLE_TYPE')
CREATE TYPE TEMP_TABLE_TYPE AS TABLE(
ERP_POID nvarchar(50),
ERRORCD nvarchar(50),
ERRORMSG nvarchar(120)
);
And use it:
exec sp_executesql #Sql, N'#TEMP_TABLE TEMP_TABLE_TYPE readonly', #TEMP_TABLE
For epxplonation you can use this link Passing table variable into dynamic sql.
If you find this way not obvious, I advise to use temporary table instead of table varible:
CREATE TABLE #TEMP_TABLE (
ERP_POID nvarchar(50),
ERRORCD nvarchar(50),
ERRORMSG nvarchar(120)
);
Related
I have an error during the execution of my proc that I must declare my variable while this last variable is well declared
because I declared my variables in the proc
Proc Syntax
USE [Test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[ADMIN_D] #LNK_Name nvarchar(60) ,#bdd varchar(50) ,#PS VARCHAR(80), #SCH SYSNAME = 'dbo'
AS
Declare #sqlm varchar(max)
Declare #sql1 nvarchar(max)
Declare #sql nvarchar(max)
Declare #sqlZ varchar(max)
Declare #LINK nvarchar(max)
Declare #LINK1 nvarchar(max)
Declare #LINK2 varchar(max)
--Declare #bdd varchar(35)
Declare #Login varchar(80)
Declare #Datetime datetime
SET NOCOUNT ON;
IF (ORIGINAL_LOGIN() IN (''))
BEGIN;
PRINT 'Déploiement interdit - Contactez l''équipe SQL ';
RETURN;
END;
SET #Datetime = Getdate()
SET #Login = system_user
SELECT #sql = Definition
FROM sys.procedures p
INNER JOIN sys.sql_modules m ON p.object_id = m.object_id
where name = #PS and p.schema_id = SCHEMA_ID(#SCH)
--BEGIN DISTRIBUTED TRAN
raiserror('--- Alim Z_CODESOURCE de Prod',0,1) WITH NOWAIT
SET #sqlZ = 'EXEC dbo.Z_CODESOURCE_Alim #NOM_OBJET = ''' + #PS + ''''
SET #LINK = N'EXEC (#sqlZ) AT ' + QUOTENAME(#LNK_Name);;
EXEC sp_executesql #LINK;
SET NOCOUNT OFF
IF EXISTS (SELECT TOP 1 1 FROM ADMIN_DEPLOY_BLOCK WHERE NOM_PS = #PS AND Nom_Schema = #SCH)
BEGIN
SELECT 'DEPLOIEMENT INTERDIT PAR ' + ACTEUR + '. RAISON: ' + RAISON
FROM ADMIN_DEPLOY_BLOCK
WHERE NOM_PS = #PS
AND Nom_Schema = #SCH;
PRINT 'Pas de déploiement !'
END
ELSE
BEGIN TRY
SET #sqlm = ' USE [' + #bdd + '];DELETE FROM METADATA WHERE NAME = '''+#PS+''''
SET #LINK = N'EXEC (#sqlm) AT ' + QUOTENAME(#LNK_Name);
EXEC sp_executesql #LINK ;
SET #sql1 = 'IF EXISTS (SELECT * FROM sys.objects WHERE type IN (''P'', ''FN'') AND name = '''+#PS+''' AND schema_id = ( select schema_id from sys.schemas WHERE name=''' + #SCH + ''' )) DROP PROCEDURE ' + #SCH + '.' + #PS
SET #sql1 = 'IF EXISTS (SELECT * FROM sys.objects WHERE type IN (''P'', ''FN'') AND name = '''+#PS+''') DROP PROCEDURE '+#PS
SET #sql1 = REPLACE(#sql1,'''','''''')
SET #sql1 = 'USE [' + #bdd + ']; EXEC(''' + #sql1 + ''')'
SET #LINK1 = N'EXEC (#sql1) AT ' + QUOTENAME(#LNK_Name) ;
EXEC sp_executesql #LINK1 ;
SET #sql = REPLACE(#sql,'''','''''')
SET #sql = 'USE [' + #bdd + ']; EXEC(''' + #sql + ''')'
SET #LINK2 = N'EXEC (#sql) AT ' + QUOTENAME(#LNK_Name) ;
EXEC sp_executesql #LINK2 ;
INSERT INTO LOG_DEPLOYPS (LOGIN, PS, DAT) values (#login, #PS, #datetime)
Print ''
Print ''
Print 'Déploiement effectuée avec succès'
--COMMIT TRAN
END TRY
BEGIN CATCH
Print ''
Print ''
Print 'Une erreur est survenue: ' + CAST(##ERROR AS VARCHAR(255))
SELECT
#PS AS ProcedureName
,ERROR_NUMBER() AS ErrorNumber
,ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage;
--ROLLBACK TRAN
END CATCH
END
Execute Proc
USE [Test]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[ADMIN_D]
#bdd = N'PROD',
#PS = N'SP_login',
#LNK_Name=N'uat-link'
SELECT 'Return Value' = #return_value
GO
Someone who can help me please
enter image description here
Run stored procedure on linked server (linked server name as variable)
The problem is these lines (and other places later on where you use the same pattern):
SET #LINK = N'EXEC (#sqlZ) AT ' + QUOTENAME(#LNK_Name);;
EXEC sp_executesql #LINK;
The sp_executesql function creates a new execution context that doesn't know about the #sqlZ variable. To get around this, there are additional arguments you must pass to the function to set up the variables and their values.
I have such script which in theory allows me to create the same Trigger for each table in DB:
DECLARE #insertTriggers VARCHAR(MAX) = '';
SELECT #insertTriggers = #insertTriggers1 + *Some Trigger creation code ending with 'GO'"
FROM INFORMATION_SCHEMA.TABLES
EXEC(#insertTriggers);
When I use PRINT and copy it to different query it works because I can seperate CREATE TRIGGERS with keyword GO. Here it doesn't work. What can I do? Do I have to make a seperate code for each Trigger?
You can use this as template to create your triggers..
DECLARE #TriggerTemplate NVARCHAR(MAX)
DECLARE #TriggerNameTemplate NVARCHAR(250)
DECLARE #Tables TABLE (ID INT IDENTITY, TableName VARCHAR(100))
INSERT #Tables
SELECT TABLE_NAME FROM INFORMATION_SCHEMA.Tables WHERE TABLE_TYPE = 'BASE TABLE'
DECLARE #ID INT, #TableName VARCHAR(100)
DECLARE #SQL NVARCHAR(MAX), #TriggerName NVARCHAR(250)
WHILE EXISTS (SELECT 1 FROM #Tables)
BEGIN
SELECT TOP 1 #ID = ID, #TableName = TableName FROM #Tables
SET #TriggerName = REPLACE(#TriggerNameTemplate,'#TableName#', QUOTENAME(#TableName))
IF EXISTS(SELECT * FROM sys.triggers WHERE name = #TriggerName)
BEGIN
SET #SQL = 'DROP TRIGGER ' + #TriggerName
EXEC (#SQL)
END
SET #SQL = REPLACE(#TriggerTemplate,'#TableName#', QUOTENAME(#TableName))
EXEC(#SQL)
DELETE #Tables WHERE ID = #ID
END
As per my comment, I developed this SP for audit purposes and it might not work for you originally, but if you alter it, I'm sure it'll assist you.
Otherwise, alter your question of what you need the triggers to do and I will then alter my answer to work for you...
if exists(select 1 from sysobjects where name = 'kv_sp_dbAudits_tr_AW')
drop procedure kv_sp_dbAudits_tr_AW
go
set ansi_nulls on
go
set quoted_identifier on
go
create procedure kv_sp_dbAudits_tr_AW(#tablename varchar(150),#switch bit)
as
BEGIN
if exists(select name from sysobjects where name = 'TempTableTriggers')
drop table TempTableTriggers;
create table TempTableTriggers (tablename varchar(100),columns varchar(max),columnvars varchar(max),columnvarst varchar(max),columnalloc varchar(max))
declare #query varchar(max)
, #loop int
, #dbname varchar(50) = (select db_name())+'_Audit'
, #table varchar(100)
, #tableau varchar(100)
, #trigger varchar(100)
, #column varchar(max)
, #columnvars varchar(max)
, #columnvarst varchar(max)
, #columnalloc varchar(max)
, #crlf char(2) = char(13)+char(10)
declare #Tables table
( id int identity primary key
, TableName varchar(100)
, TriggerName varchar(100)
, Columns varchar(max))
if #tablename like ''+'%'+''
set #tablename = #tablename
else
set #tablename = ''+#tablename+''
set #trigger = #tablename+'_Audit_kvtr_AW'
begin
if #switch = 1
begin
if exists(select name from sysobjects where name = #trigger)
begin
set #tablename = (select name from sysobjects where name = #tablename)
set #query = #crlf
set #query = #query + #crlf
set #query = #query + #crlf
set #query = #query + #crlf
set #query = #query + replicate('-',80)+#crlf
set #query = #query + #crlf
set #query = #query + 'The audit trigger "'+#tablename+'_Audit_kvtr_AW'+'" already exists!'
print (#query)
end
else
begin
if #tablename = 'all'
insert into #Tables
( TableName )
select
name
from sysobjects
where xtype = 'u'
and name not like '%audit%'
and name <> 'TempTableTriggers'
order by name
else
insert into #Tables
( TableName )
select
name
from sysobjects
where xtype = 'u'
and name not like '%audit%'
and name <> 'TempTableTriggers'
and name = #tablename
order by name
select #loop = min(id) from #Tables
while #loop is not null
begin
begin
set #query = ' declare #columns varchar(max)
select #columns = stuff((select '','' + char(10)+quotename(Column_Name)
from information_schema.columns
where table_name = '''+(select TableName from #Tables where id = #loop)+'''
and Column_Name <> ''cAllocs''
and data_type not in (''text'',''image'',''ntext'')
group by column_name, data_type, character_maximum_length, ordinal_position
order by ordinal_position
for xml path(''''), type).value(''.'', ''nvarchar(max)''),1,1,'''')
insert into TempTableTriggers (tablename,columns)
select distinct
table_name
, #columns
from information_schema.columns
where table_name = '''+(select TableName from #Tables where id = #loop)+''''
exec (#query)
end
select #loop = min(id) from #Tables where id>#loop
end
insert into #Tables
(TriggerName,TableName,Columns)
select
TableName,TableName,columns
from TempTableTriggers
select #loop = min(id) from #Tables
while #loop is not null
begin
begin
select #trigger = TriggerName+'_Audit_kvtr_AW'
, #tableau = TableName+'_Audit'
, #table = TableName
, #column = Columns
from #Tables
where id = #loop
set #query = 'create trigger '+#trigger+' on '+#table+#crlf
set #query = #query+ 'with encryption'+#crlf
set #query = #query+ 'after insert, update, delete'+#crlf
set #query = #query+ 'as'+#crlf
set #query = #query+ '/***********************************************************************************************************************************'+#crlf
set #query = #query+ 'Description : To insert any change made in '+#table+' into '+#dbname+'.dbo.'+#table+'_Audit'+#crlf
set #query = #query+ 'Author : Attie Wagner'+#crlf
set #query = #query+ 'Creation Date : 30 October 2018'+#crlf
set #query = #query+ 'Modified By : Attie Wagner'+#crlf
set #query = #query+ 'Modified Date : 28 January 2019'+#crlf
set #query = #query+ '************************************************************************************************************************************/'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'begin'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'set nocount on'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'if (select trigger_nestlevel(object_id('''+#trigger+'''))) > 1'+#crlf
set #query = #query+ 'return'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'declare #inserted varchar(15) = '''''+#crlf
set #query = #query+ 'declare #deleted varchar(15) = '''''+#crlf
set #query = #query+ 'declare #updated varchar(15) = '''''+#crlf
set #query = #query+ 'declare #action varchar(15) = '''''+#crlf
set #query = #query+ 'if((exists(select * from inserted)) and (exists(select * from deleted)))'+#crlf
set #query = #query+ ' set #updated = ''updated'''+#crlf
set #query = #query+ 'else'+#crlf
set #query = #query+ 'if(exists(select * from inserted))'+#crlf
set #query = #query+ ' set #inserted = ''new'''+#crlf
set #query = #query+ 'else'+#crlf
set #query = #query+ 'if(exists(select * from deleted))'+#crlf
set #query = #query+ ' set #deleted = ''deleted'''+#crlf
set #query = #query+ 'set #action = (select case when #inserted = ''new'' then ''new'' when #updated = ''updated'' then ''updated'' when #deleted = ''deleted'' then ''deleted'' else '''' end)'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'declare #kvAgent varchar(100) = (select cAgentName from _rtblAgents where idAgents = [dbo]._efnAgentIDFromConnection(''dbo''))'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'if #action in (''new'',''updated'')'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'begin'+#crlf
set #query = #query+ 'insert into ['+#dbname+'].dbo.'+#tableau+' ('+#column+','+#crlf+'kvUsername,'+#crlf+'kvDTStamp,'+#crlf+'kvAction)'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'select '+#column+','+#crlf+'#kvAgent,'+#crlf+'getdate(),'+#crlf+'#action'+#crlf
set #query = #query+ 'from inserted'+#crlf
set #query = #query+ 'end;'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'if #action = ''deleted'''+#crlf
set #query = #query+ #crlf
set #query = #query+ 'begin'+#crlf
set #query = #query+ 'insert into ['+#dbname+'].dbo.'+#tableau+' ('+#column+','+#crlf+'kvUsername,'+#crlf+'kvDTStamp,'+#crlf+'kvAction)'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'select '+#column+','+#crlf+'#kvAgent,'+#crlf+'getdate(),'+#crlf+'#action'+#crlf
set #query = #query+ 'from deleted'+#crlf
set #query = #query+ 'end;'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'begin'+#crlf
set #query = #query+ 'delete '+quotename(#dbname)+'.dbo.'+#table+'_Audit'+#crlf
set #query = #query+ 'where datediff(month,kvDTStamp,getdate()) > 12'+#crlf
set #query = #query+ 'end;'+#crlf
set #query = #query+ #crlf
set #query = #query+ 'end;'+#crlf
exec (#query)
end
select #loop = min(id) from #Tables where id>#loop
end
end
drop table TempTableTriggers;
end
else
begin
if #tablename = 'all'
insert into #Tables
( TriggerName )
select name from sysobjects
where xtype = 'tr'
and name like '%_Audit_kvtr_AW'
order by name
else
insert into #Tables
( TriggerName )
select name from sysobjects
where xtype = 'tr'
and name like '%_Audit_kvtr_AW'
and name = #tablename+'_Audit_kvtr_AW'
order by name
select #loop = min(id) from #Tables
while #loop is not null
begin
begin
set #trigger = (select TriggerName from #Tables where id = #loop)
set #query = 'if exists(select name from sysobjects where name = '''+#trigger+''')'+#crlf
set #query = #query+'drop trigger '+#trigger
exec (#query)
end
select #loop = min(id) from #Tables where id>#loop
end
end
end
END;
I would like to add the following stored procedure to all existing databases which contain the table schichten. All my approaches have failed so I'm looking for help here.
This is my approach:
IF object_id('tempdb.dbo.#database') is not null
drop TABLE #database
GO
CREATE TABLE #database(id INT identity primary key, name sysname)
GO
SET NOCOUNT ON
INSERT INTO #database(name)
SELECT name
FROM sys.databases
WHERE source_database_id is null
ORDER BY name
SELECT * FROM #database
DECLARE #id INT, #cnt INT, #sql NVARCHAR(MAX), #currentDb SYSNAME;
SELECT #id = 1, #cnt = max(id) FROM #database
WHILE #id <= #cnt
BEGIN
BEGIN TRY
SELECT #currentDb = name
FROM #database
WHERE id = #id
IF OBJECT_ID(#currentDb+'.dbo.schichten') IS NOT NULL
CREATE PROCEDURE #currentDb.[dbo].[Ausw_Tabelle_Taxi_Pers_Jahr]
#ColumnName nvarchar(MAX),
#Selector nvarchar(MAX),
#Gesamtergebnis nvarchar(MAX)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql1 AS NVARCHAR(MAX),
#ASSelector nvarchar(MAX),
#IFPers nvarchar(MAX);
IF #Selector = 'konz'
BEGIN
SET #ASSelector = 'Taxi'
SET #IFPers=''
END
ELSE
BEGIN
SET #ASSelector = 'Personal'
SET #IFPers = '[name] AS Name,'
END
SET #sql1 = N';WITH temp AS (SELECT *
FROM (
SELECT
ISNULL((DATENAME(m,[datum])+ cast(datepart(yyyy,[datum]) as varchar(5))),0) AS MONTHYEAR,
ISNULL(['+ #Selector +'],0) AS '+ #ASSelector +','+ #IFPers +'
ISNULL((ISNULL([umsum],0) +
ISNULL([sonst_0],0) +
ISNULL([sonst_7],0) +
ISNULL([sonst_16],0) +
ISNULL([sonst_z],0) -
ISNULL([ff],0)),0) AS UMSATZSUMME
FROM [dbo].[schichten]
) AS SOURCE
PIVOT (SUM([UMSATZSUMME]) FOR [MONTHYEAR] IN ('+ #ColumnName + N' )) AS UMSAETZE )
SELECT *, '+ #Gesamtergebnis +' AS Gesamtergebnis FROM temp ORDER BY '+ #ASSelector +''
EXEC sp_executesql #sql
END
END TRY
BEGIN CATCH
END CATCH
SET #id = #id + 1;
END
GO
I am hoping that there is someone who can help me.
You have to execute the create procedure separately, so if you wrap it up into a variable and use exec sp_executesql it should work.
IF object_id('tempdb.dbo.#database') is not null
drop TABLE #database
GO
CREATE TABLE #database(id INT identity primary key, name sysname)
GO
SET NOCOUNT ON
INSERT INTO #database(name)
SELECT name
FROM sys.databases
WHERE source_database_id is null
ORDER BY name
SELECT * FROM #database
DECLARE #id INT, #cnt INT, #sql NVARCHAR(MAX), #currentDb SYSNAME;
SELECT #id = 1, #cnt = max(id) FROM #database
WHILE #id <= #cnt
BEGIN
BEGIN TRY
SELECT #currentDb = name
FROM #database
WHERE id = #id
IF OBJECT_ID(#currentDb+'.dbo.schichten') IS NOT NULL
begin
set #sql = 'CREATE PROCEDURE '+#currentDb+'.[dbo].[Ausw_Tabelle_Taxi_Pers_Jahr]
#ColumnName nvarchar(MAX),
#Selector nvarchar(MAX),
#Gesamtergebnis nvarchar(MAX)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql1 AS NVARCHAR(MAX),
#ASSelector nvarchar(MAX),
#IFPers nvarchar(MAX);
IF #Selector = ''konz''
BEGIN
SET #ASSelector = ''Taxi''
SET #IFPers=''''
END
ELSE
BEGIN
SET #ASSelector = ''Personal''
SET #IFPers = ''[name] AS Name,''
END
SET #sql1 = N'';WITH temp AS (SELECT *
FROM (
SELECT
ISNULL((DATENAME(m,[datum])+ cast(datepart(yyyy,[datum]) as varchar(5))),0) AS MONTHYEAR,
ISNULL([''+ #Selector +''],0) AS ''+ #ASSelector +'',''+ #IFPers +''
ISNULL((ISNULL([umsum],0) +
ISNULL([sonst_0],0) +
ISNULL([sonst_7],0) +
ISNULL([sonst_16],0) +
ISNULL([sonst_z],0) -
ISNULL([ff],0)),0) AS UMSATZSUMME
FROM [dbo].[schichten]
) AS SOURCE
PIVOT (SUM([UMSATZSUMME]) FOR [MONTHYEAR] IN (''+ #ColumnName + N'' )) AS UMSAETZE )
SELECT *, ''+ #Gesamtergebnis +'' AS Gesamtergebnis FROM temp ORDER BY ''+ #ASSelector +''''
EXEC sp_executesql #sql1
END'
EXEC sp_executesql #sql
END TRY
BEGIN CATCH
END CATCH
SET #id = #id + 1;
END
GO
Assuming this is a one time need as opposed to a nightly maintenance task, you can use a built-in stored procedure, sys.sp_MSforeachdb, to execute a statement in each database. It is safe to use and has been discussed extensively on the web. However, it is an undocumented feature and can be removed without notice by Microsoft so you don't want to depend on it for recurring tasks.
Create and validate your statement in one database, then use this stored procedure to execute it in all of the databases. ? is the placeholder for the database name.
EXEC sys.sp_MSforeachdb #command1 =
'IF OBJECT_ID(''?.dbo.schichten'') IS NOT NULL
AND OBJECT_id(''?.[dbo].[Ausw_Tabelle_Taxi_Pers_Jahr]'') IS NOT NULL
BEGIN
CREATE PROCEDURE ?.[dbo].[Ausw_Tabelle_Taxi_Pers_Jahr]
#ColumnName nvarchar(MAX),
#Selector nvarchar(MAX),
#Gesamtergebnis nvarchar(MAX)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sql1 AS NVARCHAR(MAX),
#ASSelector nvarchar(MAX),
#IFPers nvarchar(MAX);
IF #Selector = ''konz''
BEGIN
SET #ASSelector = ''Taxi''
SET #IFPers=''''
END
ELSE
BEGIN
SET #ASSelector = ''Personal''
SET #IFPers = ''[name] AS Name,''
END
SET #sql1 = N'';WITH temp AS (SELECT *
FROM (
SELECT
ISNULL((DATENAME(m,[datum])+ cast(datepart(yyyy,[datum]) as varchar(5))),0) AS MONTHYEAR,
ISNULL([''+ #Selector +''],0) AS ''+ #ASSelector +'',''+ #IFPers +''
ISNULL((ISNULL([umsum],0) +
ISNULL([sonst_0],0) +
ISNULL([sonst_7],0) +
ISNULL([sonst_16],0) +
ISNULL([sonst_z],0) -
ISNULL([ff],0)),0) AS UMSATZSUMME
FROM [dbo].[schichten]
) AS SOURCE
PIVOT (SUM([UMSATZSUMME]) FOR [MONTHYEAR] IN (''+ #ColumnName + N'' )) AS UMSAETZE )
SELECT *, ''+ #Gesamtergebnis +'' AS Gesamtergebnis FROM temp ORDER BY ''+ #ASSelector +''''
EXEC sp_executesql #sql1
END
'
I need return result in terms of true or false from Stored Procedure by detecting a view contains multiple tables or not.
Attempt:
CREATE PROC spTest
#ViewName nvarchar(max)
AS
DECLARE #SQL nvarchar(max)
DECLARE #TableName nvarchar(max)
SET #SQL = 'SELECT #TableName = Table_Name
FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
WHERE View_Name = ''' + #ViewName + ''''
EXEC sp_executesql #SQL, N'#TableName nvarchar(max) OUTPUT', #TableName output
IF (#TableName > 1)
BEGIN
SELECT 'True'
END
ELSE
BEGIN
SELECT 'False'
END
GO
Note: I am not getting how to insert all tables from view into #TableName variable and check the condition.
you can achive your goal through this
create PROC spTest
#ViewName nvarchar(max)
AS
DECLARE #SQL nvarchar(max)
SET #SQL = '
DECLARE #TableName table (table_name varchar(1000))
insert into #TableName
SELECT Table_Name
FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
WHERE View_Name = ''' + #ViewName + '''
IF (select count(1) from #TableName )> 1
BEGIN
SELECT ''True''
END
ELSE
BEGIN
SELECT ''False''
END'
exec (#SQL)
GO
After creating procedure execute procedure
spTest 'pace_entity_access_view'
CREATE PROC Sptest #ViewName NVARCHAR(max)
AS
DECLARE #cnt INT
SELECT #cnt = Count(*)
FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
WHERE VIEW_NAME = #ViewName
IF ( #cnt > 1 )
BEGIN
SELECT 'True'
END
ELSE
BEGIN
SELECT 'False'
END
I have a sql stored procedure which calls another sql stored procedure. I works fine when excuted in sql server. But when I called from function in class it returns the DataTable returned by subprocedure. I want to return data returned by main procedure.
My Main Procedure:
ALTER PROCEDURE [dbo].[StoredProcedure2]
#QuaterList varchar(50) = NULL
AS
BEGIN
DECLARE #sql nvarchar(4000)
SELECT #sql='SELECT Id, Suggester, Requester, Date_Created from CRM.dbo.CRM_Doctor_Request WHERE 1=1 '
If (#QuaterList) IS NOT NULL
BEGIN
DECLARE #MonthsList varchar(1000)
EXEC dbo.SPGetMonthsListforMultipleQuaters #QuaterList, #MonthsList OUTPUT
SELECT #MonthsList
SELECT #sql=#sql + ' AND MONTH(Date_Created) in ('
+ SUBSTRING(#MonthsList, 1, LEN(#MonthsList)-1) +')'
END
EXEC sp_executesql #sql, N' #QuaterList varchar(50) ',
#QuaterList
END
Sub Procedure SPGetMonthsListforMultipleQuaters:
ALTER PROCEDURE dbo.SPGetMonthsListforMultipleQuaters
#InputList varchar(10),
#MonthsList varchar(1000) OUTPUT
AS
BEGIN
SELECT #MonthsList= ''
/* Create temp table */
CREATE TABLE #TempListTable
(
TableField int
)
/* add data into temp table */
DECLARE #TableField varchar(10), #Pos int
SET #InputList = LTRIM(RTRIM(#InputList))+ ','
SET #Pos = CHARINDEX(',', #InputList, 1)
IF REPLACE(#InputList, ',', '') <> ''
BEGIN
WHILE #Pos > 0
BEGIN
SET #TableField = LTRIM(RTRIM(LEFT(#InputList, #Pos - 1)))
IF #TableField <> ''
BEGIN
INSERT INTO #TempListTable (TableField) VALUES (CAST(#TableField AS int)) --Use Appropriate conversion
END
SET #InputList = RIGHT(#InputList, LEN(#InputList) - #Pos)
SET #Pos = CHARINDEX(',', #InputList, 1)
END
END
/* fetch data from temptable using cursor */
DECLARE #ArrayItem nvarchar(100)
DECLARE #Array_Cursor CURSOR
SET #Array_Cursor = CURSOR FAST_FORWARD FOR select TableField
from #TempListTable
OPEN #Array_Cursor
FETCH NEXT FROM #Array_Cursor INTO #ArrayItem
WHILE ##FETCH_STATUS = 0
BEGIN
/* creating list of months */
IF (#ArrayItem = 1)
SELECT #MonthsList=#MonthsList + '4,5,6,'
IF (#ArrayItem = 2)
SELECT #MonthsList=#MonthsList + '7,8,9,'
IF (#ArrayItem = 3)
SELECT #MonthsList=#MonthsList + '10,11,12,'
IF (#ArrayItem = 4)
SELECT #MonthsList=#MonthsList + '1,2,3,'
FETCH NEXT FROM #Array_Cursor INTO #ArrayItem
END
/*print #MonthsList*/
Close #Array_Cursor
deallocate #Array_Cursor
END
Problem solved. It was because I wrote SELECT #MonthsList so it was returning that too which is not required, and was also creating problems. So I removed it and problem solved.
New Procedure code is :
ALTER PROCEDURE [dbo].[StoredProcedure2]
#QuaterList varchar(50) = NULL
AS
BEGIN
DECLARE #sql nvarchar(4000)
SELECT #sql='SELECT Id, Suggester, Requester, Date_Created from CRM.dbo.CRM_Doctor_Request WHERE 1=1 '
If (#QuaterList) IS NOT NULL
BEGIN
DECLARE #MonthsList varchar(1000)
EXEC dbo.SPGetMonthsListforMultipleQuaters #QuaterList, #MonthsList OUTPUT
SELECT #sql=#sql + ' AND MONTH(Date_Created) in ('
+ SUBSTRING(#MonthsList, 1, LEN(#MonthsList)-1) +')'
END
EXEC sp_executesql #sql, N' #QuaterList varchar(50) ',
#QuaterList
END