/* When I execute this procedure in sql server it execute correctly but when I execute through coding i get this error "Incorrect syntax near set"
Thanks in advance*/
ALTER PROCEDURE [dbo].[updateCitationTrust]
#tblName varchar(50),
#updatedTableName varchar(50)
AS
DECLARE #sql NVARCHAR(4000)
Declare #ParamDefinition AS NVarchar(2000)
BEGIN
--BEGIN TRANSACTION
set #sql='Update '+#updatedTableName+ ' set [Citation Flow]=m2.[Citation Flow],'+
' [Trust Flow]=m2.[Trust Flow]'+
' FROM '+ #updatedTableName+ ' m1 '+
' INNER JOIN '+ #tblName+' m2'+
' on m1.[Linking Domain]=m2.[Item]'
Set #ParamDefinition = '#tblName varchar(50),
#updatedTableName varchar(50)'
Execute sp_Executesql #sql,
#ParamDefinition,
#tblName,
#updatedTableName
If ##ERROR <> 0 GoTo ErrorHandler
Set NoCount OFF
Return(0)
ErrorHandler:
Return(##ERROR)
END
If the object names passed as parameter values that don't confirm to the rules for regular identifiers (https://msdn.microsoft.com/en-us/library/ms175874.aspx?f=255&MSPPError=-2147217396), need to be quoted. USe the QUOTENAME function. This will also mitigate the risk of SQL injection.
ALTER PROCEDURE [dbo].[updateCitationTrust]
#tblName varchar(50),
#updatedTableName varchar(50)
AS
DECLARE #sql NVARCHAR(4000)
Declare #ParamDefinition AS NVarchar(2000)
BEGIN
--BEGIN TRANSACTION
set #sql='Update '+QUOTENAME(#updatedTableName)+ ' set [Citation Flow]=m2.[Citation Flow],'+
' [Trust Flow]=m2.[Trust Flow]'+
' FROM '+ QUOTENAME(#updatedTableName)+ ' m1 '+
' INNER JOIN '+ QUOTENAME(#tblName)+' m2'+
' on m1.[Linking Domain]=m2.[Item]'
Set #ParamDefinition = '#tblName varchar(50),
#updatedTableName varchar(50)'
Execute sp_Executesql #sql,
#ParamDefinition,
#tblName,
#updatedTableName
If ##ERROR <> 0 GoTo ErrorHandler
Set NoCount OFF
Return(0)
ErrorHandler:
Return(##ERROR)
END
Related
create PROC usp_delete_qu (
#table NVARCHAR(128),
#new_date datetime) AS BEGIN
DECLARE #sql NVARCHAR(MAX);
-- construct SQL
SET #sql = N'delete FROM ' + #table + N' where modified_date < ' +#new_date
-- execute the SQL
EXEC sp_executesql #sql;
END;
I create a stored procedure which I need to delete rows with modified date. But while I tried to execute this I got error:
Msg 241, Level 16, State 1, Procedure usp_query, Line 10 [Batch Start Line 19]
Conversion failed when converting date and/or time from character string.
My intention is to use this stored procedure to delete rows in all tables in my database.
Just use a parameter in your dynamic statement:
CREATE PROCEDURE usp_delete_qu
#table NVARCHAR(128),
#new_date datetime
AS BEGIN
DECLARE #sql NVARCHAR(MAX);
DECLARE #rc int
-- construct SQL
SET #sql = N'delete FROM ' + QUOTENAME(#table) + N' where modified_date < #new_date'
-- execute the SQL
EXEC #rc = sp_executesql #sql, N'#new_date datetime', #new_date
IF #rc <> 0 PRINT 'Error'
END
You'll have to convert your date into string when using dynamic query.
create PROC usp_delete_qu (
#table NVARCHAR(128),
#new_date datetime) AS BEGIN
DECLARE #sql NVARCHAR(MAX);
-- construct SQL
SET #sql = N'delete FROM ' + #table + N' where modified_date < ''' + CONVERT(NVARCHAR(50), #new_date) + ''' '
-- execute the SQL
EXEC sp_executesql #sql;
END;
CREATE PROCEDURE [lot].[wipeAll]
#num VARCHAR(16),
#quotaType INT,
#TableName NVARCHAR(128)
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM #TableName WITH (TABLOCKX)
WHERE num = #num AND quotaType = #quotaType;
END
I can't figure out how to work around this error I'm getting
Incorrect syntax near Tablockx . Expecting '(' or Select
from the above code snippet. Any help would be greatly appreciated!
You need to use dynamic SQL for this:
BEGIN
SET NOCOUNT ON;
DECLARE #sql NVARCHAR(MAX);
SET #sql = '
DELETE FROM #TableName WITH (TABLOCKX) WHERE num=#num AND quotaType=#quotaType)';
SET #sql = REPLACE(#sql, '#TableName', #TableName);
EXEC sp_executesql #sql,
'#num varchar(16), #quotaType int',
#num=#num, #quotaType=quotaType
END;
Can we assign string functions Like(Patindex,charindex) along with select command to a dynamic sql variable ?
here is an example that i want assign to a dynamic sql varaible
CREATE PROCEDURE Dynamic_SP
#Table_Name sysname
AS
BEGIN
SET NOCOUNT ON;
DECLARE #DynamicSQL nvarchar(4000)
SET #DynamicSQL = 'SELECT patindex('%,%',ColumnName) FROM '+ #Table_Name
EXECUTE sp_executesql #DynamicSQL
END
GO
EXEC Dynamic_SP 'tblFilm'
i konow it will raise an error at assigning query to the #DynamicSQL ! But is there any way i can do ??
try this:
CREATE PROCEDURE Dynamic_SP
#Table_Name sysname
#ColumnName sysname
AS
BEGIN
SET NOCOUNT ON;
DECLARE #DynamicSQL nvarchar(4000)
SET #DynamicSQL = 'SELECT patindex(''%,%'',' + #ColumnName + ') FROM '+ #Table_Name
-- PRINT (#DynamicSQL) --comment in this and comment out the EXEC(#DynamicSQL) to check whether the query looks good
EXEC (#DynamicSQL)
END
GO
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