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)
);
I have a problem here, I'm searching in stackoverflow but still not find the best way
i have a stored procedure (SP) like this
DECLARE #table NVARCHAR(max), #SQLQuery NVARCHAR(max)
SET #table = #meta+'_prize4winner'
SET #SQLQuery = 'if exists (Select * from [dbo].' + #table + ' where idCampaignLog =''' + convert(nvarchar, #ID) +''') exec InsertC2CWinner''' + convert(nvarchar, #meta) +''','''+ convert(nvarchar, #ID)+''',null else select ''you lose ''as winStatus'
execute SP_EXECUTESQL #SQLQuery
need help how and where to set the output if I want the output if exist 'YOU WIN' else 'You Lose' thx
The general syntax is like this
DECLARE #retval int
DECLARE #sSQL nvarchar(500);
DECLARE #ParmDefinition nvarchar(500);
DECLARE #tablename nvarchar(50)
SELECT #tablename = N'products'
SELECT #sSQL = N'SELECT #retvalOUT = MAX(ID) FROM ' + #tablename;
SET #ParmDefinition = N'#retvalOUT int OUTPUT';
EXEC sp_executesql #sSQL, #ParmDefinition, #retvalOUT=#retval OUTPUT;
SELECT #retval;
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 want to pass table name as parameter and I want to that parameter in where clause
CREATE PROC [dbo].[bb_GetPrepositionInfo]
#userid INT,#propId INT,#tabName varchar(50)
AS
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRAN
SELECT *
FROM #tblname
WHERE ([acq_id] = #propId AND [user_id] = #userid)
COMMIT
GO
Not tested but You need to use Dynamic SQL.
CREATE PROC [dbo].[bb_GetPrepositionInfo]
#userid INT,#propId INT,#tabName varchar(50)
AS
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRAN
DECLARE #SQL varchar(250)
SELECT #SQL = 'SELECT * FROM ' + QuoteName(#tabName) + ' where acq_id=' + Quotename(#propId) + ' AND user_id=' + Quotename(#userid)
EXEC (#SQL)
COMMIT
GO
CREATE PROC [dbo].[bb_GetPrepositionInfo]
DECLARE #userid INT
DECLARE #propId INT
DECLARE #tabName varchar(50)
DECLARE #sqlCommand varchar(200)
AS
SET NOCOUNT ON
SET XACT_ABORT ON
BEGIN TRAN
SET #sqlCommand = 'SELECT * from ' + #tabName +'WHERE [acq_id]='+ #propId +'AND [user_id] = '+ #userid
EXEC (#sqlCommand)
COMMIT
GO
I am trying to insert ID values in stored procedure from .net and the int value for ID is inserting negative value in the stored procedure.
But when a negative value is passed its giving me an error incorrect syntax near '*'.
Please help me.
Here is my stored procedure
ALTER PROCEDURE [dbo].[HotlinePlusAdministration_ArticleMigrator]
#Id AS INT,
--#CategoryID AS INT,
--#Title AS Varchar(200),
--#ArticleDate AS datetime,
#DestLinkServer AS VARCHAR(50),
#UserID AS VARCHAR(8),
#ReturnMsg AS VARCHAR(1000) OUTPUT
AS
BEGIN
DECLARE #Query AS NVARCHAR(4000)
DECLARE #Log AS VARCHAR(8000)
DECLARE #ArticleID as int
DECLARE #NewArticleID as int
DECLARE #ArticleKeyExists as int
DECLARE #Title as varchar(200)
DECLARE #CategoryID as INT
DECLARE #ArticleDate as varchar(30)
DECLARE #ParmDefinition nvarchar(500);
SET XACT_ABORT ON -- Required for nested transaction
BEGIN TRAN
-- Check if ArticleID exists in Destination Server
SET #Query = N' SELECT #ArticleKeyExists = COUNT(*)
FROM ' + #DestLinkServer + '.HL2_61.dbo.Article' + ' where ArticleKey = ' + str(#Id)
SET #ParmDefinition = N' #ID int, #ArticleKeyExists int OUTPUT';
EXECUTE sp_executesql #Query , #ParmDefinition, #ID, #ArticleKeyExists OUTPUT;
--EXECUTE sp_executesql 1234,'BRHLSQL8','BRWSQLDC',#return = retnmsg
IF ##ERROR <> 0
BEGIN
ROLLBACK TRANSACTION
SET #ReturnMsg = #Log + '<span style="color:red;">ERROR: <br></span>'
RETURN -1
END
--Delete existing Articles for select page
set #Query = 'DELETE FROM ' + #DestLinkServer +
'.HL2_61.dbo.Article ' +
'WHERE ArticleKey = ' + CONVERT(VARCHAR, #Id)
--'WHERE CategoryID = ' + CONVERT(VARCHAR, #CategoryID) + ' and Title = ''' + #Title + ''' and ArticleDate = ''' + #ArticleDate + ''''
Print #Query
EXEC(#Query)
When I am executing the code as below I am getting the error.
DECLARE #return_value int,
#ReturnMsg varchar(1000)
EXEC #return_value = [dbo].[Migrator]
#Id = -1591276581,
#DestLinkServer = N'SQLDC',
#UserID = N'10c1',
#ReturnMsg = #ReturnMsg OUTPUT
SELECT #ReturnMsg as N'#ReturnMsg'
SELECT 'Return Value' = #return_value
GO
Please someone help me..
Thanks
When you convert an int to a varchar, you have to specify the size:
Try this:
CONVERT(VARCHAR(50), #Id)
and avoid using str(#Id)