Select more values on clause IF in the stored procedure - sql

I have a problem of syntax in my stored procedure when a select more values in my IF, on code snippet 37 OR 35.
IF 37 OR 35 NOT IN (SELECT CodPerfilSistema
FROM [BRSAODB09].[ADMIN].[dbo].[PERFIL_USUARIO]
WHERE CodUsuario = #pCodUsuario)
BEGIN
SET #CondicaoEmpresa = ' AND [Lote].CodEmpresa = ' + CONVERT(varchar(10), #pCodEmpresa);
SET #CondicaoProjeto = ' AND [AUD_Projeto].CodProjeto = ' + CONVERT(varchar(10), #pCodProjeto);
END
I tried the code above.
And below is how the code is running
IF 37 NOT IN (SELECT CodPerfilSistema
FROM [BRSAODB09].[ADMIN].[dbo].[PERFIL_USUARIO]
WHERE CodUsuario = #pCodUsuario)
BEGIN
SET #CondicaoEmpresa = ' AND [Lote].CodEmpresa = ' + CONVERT(varchar(10), #pCodEmpresa);
SET #CondicaoProjeto = ' AND [AUD_Projeto].CodProjeto = ' + CONVERT(varchar(10), #pCodProjeto);
END
I need the permission for Id 37 and 35.

try the below
IF not exists (SELECT * FROM [BRSAODB09].[ADMIN].[dbo].[PERFIL_USUARIO] where CodUsuario = #pCodUsuario
and CodPerfilSistema in (37,35))
BEGIN
SET #CondicaoEmpresa = ' AND [Lote].CodEmpresa = ' + CONVERT(varchar(10), #pCodEmpresa);
SET #CondicaoProjeto = ' AND [AUD_Projeto].CodProjeto = ' + CONVERT(varchar(10), #pCodProjeto);
END

Thanks guys for the help, I found a way to solve the problem I had.
It stayed like this:
IF NOT EXISTS(
SELECT 1
FROM [BRSAODB09].[ADMIN].[dbo].[PERFIL_USUARIO]
WHERE CodUsuario = #pCodUsuario
AND CodPerfilSistema IN(37,35)
)
BEGIN
SET #CondicaoEmpresa = ' AND [Lote].CodEmpresa = ' + CONVERT(varchar(10),
#pCodEmpresa);
SET #CondicaoProjeto = ' AND [AUD_Projeto].CodProjeto = ' + CONVERT(varchar(10),
#pCodProjeto);
END

Related

How to update table with while loop in Stored Procedure?

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"

Dynamic query SQL where condition

I want to attach numeric value in dynamic query but I am an getting error:
Conversion failed when converting the varchar value ' + #psRegionCode + ' to data type smallint
My query is:
SET #psRegionCode = UPPER(LTRIM(RTRIM(#psRegionCode)))
IF (#psRegionCode <> 0)
BEGIN
SET #sSQLStr = #sSQLStr + ' ' + 'AND reg.region_cd = ''' + #psRegionCode + ''''
END
Things which I tried:
SET #psRegionCode = UPPER(LTRIM(RTRIM(#psRegionCode)))
IF (#psRegionCode <> 0)
BEGIN
SET #sSQLStr = #sSQLStr + ' ' +
'AND reg.region_cd = ' + cast(#psRegionCode as nvarchar(10) ''
END
Can somone please help me with this?
IF (#psRegionCode <> 0)
BEGIN
SET #sSQLStr = #sSQLStr +
' AND reg.region_cd = ' + cast(#psRegionCode as nvarchar(10))
you need to make sure you have correct number of apostrophes
if #psRegionCode is number, why you ltrim it? if it is a string why you cast it?
Well, by your syntax I'm guessing SQL-Server? Any way I think your second query should work, you are just missing a parenthesis :
IF (#psRegionCode <> 0)
BEGIN
SET #sSQLStr = #sSQLStr +
' AND reg.region_cd = ' + cast(#psRegionCode as nvarchar(10))
END
You can't concat a number into a string without the conversion.

How to optimize nested conditional SQL Server Query 2008R2

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

SQL Error The multi-part identifier could not be bound

When I'm trying to execute this query, I get "The multi-part identifier "#EachEmployee.ResultID" could not be bound." error.
DECLARE #QueryText VARCHAR(1000)
SET #QueryText = '
UPDATE #EachEmployee2
SET #EachEmployee2.CorrectAnswerCount = (
SELECT COUNT (TMID)
FROM
' + #WorkDatabaseName + '.dbo.TestBlockTM AS TBTM,
' + #WorkDatabaseName + '.dbo.TestResultTM AS TRTM
WHERE 1 = 1
AND TBTM.TMID = TRTM.OtvetID
AND TBTM.TMPID = TRTM.VoprosID
AND TRTM.TestResultID = #EachEmployee2.ResultID
)
WHERE
#EachEmployee2.IsGroup = 0 AND #EachEmployee2.BlockID = 1'
EXECUTE(#QueryText)
However, the similar query is working perfectly:
UPDATE #EachEmployee2
SET #EachEmployee2.ResultID = (
SELECT TOP 1 TestResultID
FROM #AnswersList AS a
WHERE 1 = 1
AND a.SID = #EachEmployee2.SID
AND a.UserID = #EachEmployee2.UserID
)
Can someone tell what's a problem here? Thanks and appreciate your help.
try this
SET #QueryText = '
UPDATE emp
SET emp.CorrectAnswerCount = (
SELECT COUNT (TMID)
FROM
' + #WorkDatabaseName + '.dbo.TestBlockTM AS TBTM,
' + #WorkDatabaseName + '.dbo.TestResultTM AS TRTM
WHERE 1 = 1
AND TBTM.TMID = TRTM.OtvetID
AND TBTM.TMPID = TRTM.VoprosID
AND TRTM.TestResultID = emp.ResultID
)
From #EachEmployee2 emp
WHERE
emp.IsGroup = 0 AND emp.BlockID = 1'
Can you try this query and tell me if the error still occurs:
SET #QueryText = '
UPDATE #EachEmployee2
SET CorrectAnswerCount = (
SELECT COUNT (TMID)
FROM
' + #WorkDatabaseName + '.dbo.TestBlockTM AS TBTM,
INNER JOIN ' + #WorkDatabaseName + '.dbo.TestResultTM AS TRTM ON TRTM.OtvetID = TBTM.TMID
AND TRTM.VoprosID = TBTM.TMPID
AND TRTM.TestResultID = E.ResultID
)
FROM #EachEmployee2 E
WHERE E.IsGroup = 0
AND E.BlockID = 1'
I added the FROM clause and modified a bit the subquery in order to replace the old SQL syntax by an explicit INNER JOIN clause.
Hope this will help

MS SQL Stored Procedure run time error

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).