How to rectify error in sp while concatenating the MSSQL queries?
SET #Ws_Sql = 'SELECT CONVERT(VARCHAR(25),#WS_STATUS) AS [Status],A.RECNUB AS [Rec #], A.REVCOD AS [Revenue CODE], '
IF #LNKOPT = 1
BEGIN
SET #Ws_Sql = #WS_SQL +''' AS [Status],'
END
ELSE IF #LNKOPT = 2
BEGIN
SET #Ws_Sql = #WS_SQL +'' + 'Modified' + ' AS [Status],'
END
ELSE IF #LNKOPT = 3
BEGIN
SET #Ws_Sql = #WS_SQL +'' + 'Deleted' + ' AS [Status],'
END
ELSE IF #LNKOPT = 4
BEGIN
SET #Ws_Sql = #WS_SQL +'' + 'Reprint' + ' AS [Status],'
END
Are you attempting to assign an empty value to the [Status] output column, in case #LNKOPT = 1?
In that case, you need to use double apostrophes, to escape the apostrophe character:
IF #LNKOPT = 1
BEGIN
SET #Ws_Sql = #WS_SQL +''''' AS [Status],'
END
which will result in #Ws_Sql containing: '' AS [Status] (otherwise it would result in ' AS [Status] which will give an error when the #Ws_Sql string is executed).
Related
I want to update my dynamic table in while loop.
Here is my code:
While (#j<=#tmp_getRowCount)
Begin
Set #firstcolumn = (Select SplitFirst_tblAR from #result_AR Where rownumber = #j) //String//
Set #secondcolumn = (Select EMail_tblAR from #result_AR Where rownumber = #j) //String//
Set #thirdcolumn = (Select SplitFirst_tblKul from #result_AR Where rownumber = #j) //String//
Set #fourthcolumn = (Select EMail_tblKul from #result_AR Where rownumber = #j) //String//
insert into #test Values(#tmp_ID, #firstcolumn,#secondcolumn,#thirdcolumn,#fourthcolumn)
if ((#firstcolumn = #thirdcolumn) AND (#secondcolumn != #fourthcolumn) AND (#firstcolumn != ''))
begin
Set #q_updateTable = 'Update '+ quotename(#tablename) +' Set '+#columnname+' = ''' + #fourthcolumn + ''' Where ID = ' + #tmp_ID + ''
Exec sp_executesql #q_updateTable
end
SET #j = #j+1
End
My result_AR table:
I know the error is in here:
Where ID = ' + #tmp_ID + ''
When I change this Where clause as,
Where '+#columnname+' = ''' + #secondcolumn + ''' '
code works correctly.
Why can't I set as ID my where clause? I am getting ID value as integer.
The error is 'Query completed with errors'.
Thanks in advance.
you can not set Id in where clause because the id is integer value and you are concatenating it with string (varchar).
So first you have to convert it in (String)varchar and the you can use it where clause.
Like :
Set #q_updateTable = 'Update '+ quotename(#tablename) +' Set '+#columnname+' = ''' + #fourthcolumn + ''' Where ID = ' + convert(varchar,#tmp_ID) + ''
Exec sp_executesql #q_updateTable
you have to use "convert(varchar,#tmp_ID)" insted of "#tmp_ID"
if {condition1} = '0'
begin
if {condition2} = 'Yes'
Begin
Set #SQLQuery = #SQLQuery + ' AND '
end
else
begin
Set #SQLQuery = #SQLQuery + ' AND '
end
end
As #Pranav states in the comments, you can do the following:
if {condition1} = '0'
Set #SQLQuery = #SQLQuery + ' AND ' +
CASE WHEN {condition2} = 'Yes' THEN <whatever cond2=Yes>
ELSE <whatever cond2<>Yes> END
hope this helps, just assign any or empty value to #SQLQuery before running this
SELECT #SQLQuery+ = CASE WHEN CONDITION1='0' AND CONDITION2='Yes' THEN
' AND '
WHEN CONDITION1='0' THEN
' AND '
END
New to triggers and trying to figure out why the nested IF at the end of this of this trigger doesn't seem to be recursive. What I would like is for it to trigger itself over and over again until it hits a #ProcessStatus of 4. It only goes to 1. Not really sure why, any suggestions?
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tr_ProcessRecord] ON [dbo].[_ShipMain]
FOR UPDATE
AS
BEGIN TRANSACTION
declare #cqID nvarchar(24),
#carrier nvarchar(24),
#shipCode nvarchar(24),
#Note nvarchar(255),
#ProcessStatus nvarchar(24);
SELECT #cqID = i.ConnectQueryID from inserted i;
SELECT #carrier = i.Carrier from inserted i;
SELECT #shipCode = i.ShipCode from inserted i;
SELECT #ProcessStatus = i.Status from inserted i;
SELECT #Note = 'ID: ' + #cqID + ' ' +
'Carrier: ' + ' ' + convert(nvarchar(24), #carrier) + ' ' +
'Ship Code: ' + #shipCode + ' ' +
(SELECT CASE #ProcessStatus
WHEN 0 THEN 'Recieved'
WHEN 1 THEN 'Processing'
WHEN 2 THEN 'More Processing'
WHEN 3 THEN 'Finishing'
WHEN 4 THEN 'Processed'
END);
INSERT INTO [_ShipLog]
SELECT #cqID
,GETDATE()
,#Note
,#ProcessStatus
IF #ProcessStatus = 0
BEGIN
UPDATE [_ShipMain]
SET [Status] = #ProcessStatus + 1
WHERE [ConnectQueryID] = #cqID;
END
IF #ProcessStatus = 1
BEGIN
UPDATE [_ShipMain]
SET [Status] = #ProcessStatus + 1
WHERE [ConnectQueryID] = #cqID;
END
IF #ProcessStatus = 2
BEGIN
UPDATE [_ShipMain]
SET [Status] = #ProcessStatus + 1
WHERE [ConnectQueryID] = #cqID;
END
IF #ProcessStatus = 3
BEGIN
UPDATE [_ShipMain]
SET [Status] = #ProcessStatus + 1
WHERE [ConnectQueryID] = #cqID;
END
COMMIT TRANSACTION
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[tr_ProcessRecord] ON [dbo].[_ShipMain]
FOR UPDATE
AS
BEGIN TRANSACTION
declare #cqID nvarchar(24),
#carrier nvarchar(24),
#shipCode nvarchar(24),
#Note nvarchar(255),
#ProcessStatus nvarchar(24);
SELECT
#cqID = i.ConnectQueryID
,#carrier = i.Carrier
,#shipCode = i.ShipCode
,#ProcessStatus = i.Status
from inserted i;
SELECT #Note = 'ID: ' + #cqID + ' ' +
'Carrier: ' + ' ' + convert(nvarchar(24), #carrier) + ' ' +
'Ship Code: ' + #shipCode + ' ' +
(SELECT CASE #ProcessStatus
WHEN 0 THEN 'Recieved'
WHEN 1 THEN 'Processing'
WHEN 2 THEN 'More Processing'
WHEN 3 THEN 'Finishing'
WHEN 4 THEN 'Processed'
END);
INSERT INTO [_ShipLog]
SELECT #cqID
,GETDATE()
,#Note
,#ProcessStatus
UPDATE [_ShipMain]
SET [Status] = #ProcessStatus + 1
WHERE [ConnectQueryID] = #cqID
and #ProcessStatus in (0,1,2,3)
COMMIT TRANSACTION
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I escape a single quote in sqlserver?
I got a script below that drop everything on the database from this link. It does error when I execute on this line.
SET #statement = '
IF(#type = 'F') or (#type = 'C') or (#type = 'D') or (#type='F') or (#type='K')
The reason is because the single quote. I want to know how can I fix this error?
/*** drop (pretty much) everything before rebuilding the database ***/
DECLARE
OBJECTS CURSOR FOR SELECT
so.name,
so.type,
so.type_desc,
p.name AS parentName
FROM
sys.objects AS so
LEFT JOIN sys.objects AS p ON so.parent_object_id = p.object_id
WHERE
so.schema_id = 1
ORDER BY
CASE
WHEN so.type = 'F' THEN
0
WHEN so.type = 'TR' THEN
1
WHEN so.type = 'U' THEN
2
WHEN so.type = 'F' THEN
3
ELSE
4
END OPEN OBJECTS DECLARE
#name AS nvarchar (MAX) DECLARE
#type AS nvarchar (2) DECLARE
#type_desc AS nvarchar DECLARE
#parentName AS nvarchar (MAX) DECLARE
#statement AS nvarchar (MAX) FETCH NEXT
FROM
OBJECTS INTO #name,
#type,
#type_desc,
#parentName
WHILE ##FETCH_STATUS = 0
BEGIN
SET #statement = ' IF(#type = ' F ')
BEGIN
PRINT ' DROPING FK : ' + #name + ' OF type ' + #type + ' (' + #type_desc + ') '
SET #statement = ' ALTER TABLE ' + #parentName + ' DROP CONSTRAINT ' + #name
EXECUTE(#statement)
END
ELSE IF (#type = ' TR ')
BEGIN
PRINT ' DROPING TRIGGER : ' + #name + ' OF type ' + #type + ' (' + #type_desc + ') '
SET #statement = ' DROP TRIGGER ' + #name
EXECUTE(#statement)
END
ELSE IF (#type = ' U ')
BEGIN
PRINT ' DROPING TABLE : ' + #name + ' OF type ' + #type + ' (' + #type_desc + ') '
SET #statement = ' DROP TABLE ' + #name
EXECUTE(#statement)
END
ELSE IF (#type = ' FN ')
BEGIN
PRINT ' DROPING FUNCTION : ' + #name + ' OF type ' + #type + ' (' + #type_desc + ') '
SET #statement = ' DROP FUNCTION ' + #name
EXECUTE(#statement)
END
ELSE
PRINT ' Didn 't drop object ' + #name + ' of type ' + #type + ' (' + #type_desc + ')' FETCH NEXT
FROM
OBJECTS INTO #name,
#type,
#type_desc,
#parentName
END CLOSE OBJECTS DEALLOCATE OBJECTS
if you want to use single quote inside a prepared statement, escape it with another single quote, example,
SET #statement = 'world''s view';
SET #statement2 = 'world''s view';
from your example above
SET #statement = '
IF(#type = ''F'') or (#type = ''C'') or
(#type = ''D'') or (#type=''F'') or
(#type=''K'')'
-- the strings are all red.
Single quote is used to represent a string literal in SQL.
If you need to explicitly insert a single quote , you should use double single quotes ('')
It should be like this:
SET #statement = 'IF(#type = ''F'') or (#type = ''C'') or (#type = ''D'') or (#type=''F'') or (#type=''K'')'
Raj
For the fiscal year parameter I have 2010, 2011 and 2012 as an option but no matter what year I select the "submitted On" column still shows 2011 dates. I want for example if I select 2010, only data submitted on 2010 should show and likewise for all the years. Any help?
set #sqlstr = '
Select
[SchoolName] As [School Name],
Status= CASE WHEN PS.PrivateSchoolID = Ed.PrivateSchoolID THEN ''Submitted''
ELSE ''Not Submitted''END,
[Submitted By]= CASE WHEN PS.PrivateSchoolID = Ed.PrivateSchoolID
THEN [FirstName] + '' '' + [LastName] ELSE NULL END,
[Submitted On]= CASE WHEN PS.PrivateSchoolID = Ed.PrivateSchoolID
THEN Convert( Varchar(10), Ed.CreatedDate, 101 ) ELSE NULL END
From EnrollmentDateSchool Ed Right Outer Join
(select FP.FiscalYear, PrivateSchool.* from PrivateSchool
INNER JOIN FiscalYearPrivateSchool FP
ON PrivateSchool.PrivateSchoolID = FP.PrivateSchoolID) PS
ON Ed.PrivateSchoolID = PS.PrivateSchoolID Left Outer Join
Finance.dbo.Person P ON Ed.CreatedBy = P.PersonID
Where (FiscalYear = '+convert(varchar, #FiscalYear)+') AND (PS.IsActive = 1)'
IF (#SchoolID != -1)
set #sqlstr = #sqlstr + ' AND SchoolID ='+ convert(varchar, #SchoolID)
IF (#Status = -1)
set #sqlstr = #sqlstr + ' AND (PS.PrivateSchoolID = PS.PrivateSchoolID' + ')'
ELSE IF (#Status = 1)
set #sqlstr = #sqlstr + ' AND (PS.PrivateSchoolID = Ed.PrivateSchoolID' + ')'
ELSE
set #sqlstr = #sqlstr + 'AND (Ed.PrivateSchoolID is null' + ')'
SET #sqlstr = #sqlstr + 'ORDER BY SchoolName'
EXEC(#sqlstr)
END
There is no need at all for dynamic sql here.
SELECT
...
FROM ...
WHERE FiscalYear=#FiscalYear AND PS.IsActive=1
AND (#SchoolID = -1 OR SchoolID=#SchoolID)
AND ( (#Status = -1)
OR (#Status=1 AND PS.PrivateSchoolID = Ed.PrivateSchoolID)
OR (Ed.PrivateSchoolID is null) )
ORDER BY SchoolName