I have a issue resulting in the following error.
declare #transDate datetime = GETDATE();
declare #main_work_center as varchar(50) = 'PMOM';
declare #team as varchar(1) = '';
declare #full_work_center as varchar(50);
IF(#team = '' OR #team IS NULL) then
#full_work_center = #main_work_center;
else
#full_work_center = #main_work_center + #team;
end if
THis is the error: Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'then'.
You have multiple syntax errors in your code, that's just the first.
then is not allowed with an if
You need a set before your variable assignments in the if/else
There is no if after the end
I like to wrap the code inside the two blocks of an if/else with begin and end, much like the curly braces in other languages, for clarity.
Fully corrected code:
declare #transDate datetime = GETDATE();
declare #main_work_center as varchar(50) = 'PMOM';
declare #team as varchar(1) = '';
declare #full_work_center as varchar(50);
IF(#team = '' OR #team IS NULL)
begin
set #full_work_center = #main_work_center;
end
else
begin
set #full_work_center = #main_work_center + #team;
end;
Related
I'm getting error while executing below mentioned query in DB2.
CREATE OR REPLACE FUNCTION ETLUAT.FUNC_GET_CLAIM_STATUS(IN i_claim_no Varchar(4000))
RETURNS varchar(4000)
BEGIN
DECLARE tempReasonid varchar(50);
DECLARE tempReasontxt varchar(5000);
DECLARE txtreason varchar(4000);
DECLARE tempStatus varchar(1);
DECLARE tempUpdateNo int;
DECLARE tempOutPut varchar(2000);
DECLARE tempCnt int;
DECLARE tempPayStatus int;
DECLARE tempPayStatusChar varchar(25);
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
RETURN 'OTHERS';
END;
Select A.TXT_CLAIM_STATUS, a.num_update_no into tempStatus, tempUpdateNo From ETLUAT.PRESTG_GC_CLAIM_INFO A
where A.Num_Update_No =(Select Max(Num_Update_No) From ETLUAT.PRESTG_GC_CLAIM_INFO G Where G.Num_Claim_No = i_claim_no) and a.num_claim_no = i_claim_no;
if tempStatus = 'N' then SET tempOutPut = 'Notification';
elseif tempStatus = 'I' then
begin
select count(1) into tempCnt from ETLUAT.PRESTG_GC_CLAIM_INFO where TXT_CLAIM_STATUS = 'I'
AND NUM_CLAIM_NO = i_claim_no and NUM_UPDATE_NO >(select max(NUM_UPDATE_NO) From ETLUAT.PRESTG_GC_CLAIM_INFO
where TXT_CLAIM_STATUS = 'N' AND NUM_CLAIM_NO = i_claim_no);
if tempCnt = 1 then SET tempOutPut = 'Registration';
elseif tempCnt > 1 then SET tempOutPut = 'Processing';
else SET tempOutPut = 'Others';
end if;
return tempOutPut;
END;
end if;
return tempOutPut;
END;
error:SQL Error [42601]: An unexpected token "END-OF-STATEMENT" was found following "END". Expected tokens may include: "JOIN <joined_table>".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.9.78
please anyone help me to figure out my mistake.
I'm writing a stored procedure that will be executed from C# to get data from database. Therefore I have to pass a GUID to this stored procedure and it should find data in table Contact or in the Lead table & return data back to C# app via output parameters.
When I try to execute this stored procedure in SSMS, I get a SQL exception
Must declare the scalar variable "#LastName"
Code:
ALTER PROCEDURE [api].[GetUser_NetId]
#NetId uniqueidentifier
, #LastName nvarchar(200) = '' OUTPUT
, #FirstName nvarchar(200) = '' OUTPUT
, #Country uniqueidentifier = NULL OUTPUT
, #Newsletter bit = 0 OUTPUT
AS
DECLARE
#Table SMALLINT
SET #Table = (
SELECT MIN(T.ID) FROM (
SELECT 100 AS [ID] FROM dbo.Contact WHERE Net_ID = #NetId
UNION ALL
SELECT 200 AS [ID] FROM dbo.Lead WHERE Net_ID = #NetId
) T
)
DECLARE #SQL NVARCHAR(MAX)
SET #SQL = CONCAT(
' SELECT
#LastName = tbl.LastName,
#FirstName = tbl.FirstName,
#Country = tbl.Address1CountryId,
#Newsletter = tbl.Newsletter,
FROM
dbo.'
, CASE #Table
WHEN 100 THEN 'Contact'
WHEN 200 THEN 'Lead'
END
, ' as tbl
WHERE 1=1
AND tbl.Net_Id = '''
, #NetId
, ''''
)
EXEC(#SQL)
..a slightly simpler approach
ALTER PROCEDURE [api].[GetUser_NetId]
#NetId uniqueidentifier
, #LastName nvarchar(200) = '' OUTPUT
, #FirstName nvarchar(200) = '' OUTPUT
, #Country uniqueidentifier = NULL OUTPUT
, #Newsletter bit = 0 OUTPUT
AS
BEGIN
IF EXISTS(SELECT * FROM dbo.Contact WHERE Net_ID = #NetId)
BEGIN
SELECT
#LastName = tbl.LastName,
#FirstName = tbl.FirstName,
#Country = tbl.Address1CountryId,
#Newsletter = tbl.Newsletter
FROM dbo.Contact WHERE Net_ID = #NetId;
END
ELSE
BEGIN
SELECT
#LastName = tbl.LastName,
#FirstName = tbl.FirstName,
#Country = tbl.Address1CountryId,
#Newsletter = tbl.Newsletter
FROM dbo.Lead WHERE Net_ID = #NetId;
END
END
I am getting an error like:
Msg 137, Level 15, State 2, Line 18
Must declare the scalar variable "#CustomerKey".
Msg 137, Level 15, State 1, Line 21
Must declare the scalar variable "#FirstName".
Msg 137, Level 15, State 1, Line 30
Must declare the scalar variable "#FirstName".
IF EXISTS(SELECT * FROM VW_FactInternetSales WHERE CustomerKey = #CustomerKey)
BEGIN
SELECT
#FirstName = .FirstName,
#TaxAmt = .TaxAmt,
#Country = .Country,
#CustomerKey = .CustomerKey
FROM DimCustomer WHERE CustomerKey = #CustomerKey
END
ELSE
BEGIN
SELECT
#FirstName = .FirstName,
#TaxAmt = .TaxAmt,
#Country = .Country,
#CustomerKey = .CustomerKey
FROM VW_FactInternetSales WHERE CustomerKey = #CustomerKey
END
END
I cant add my table in this line
#FirstName = .FirstName,
#TaxAmt = .TaxAmt,
#Country = .Country,
#CustomerKey = .CustomerKey
I have a function, which needs to return true if there is a row in a table with values which user inputs.
I need to write the value to a variable which Ii return, but I got an error:
Incorrect syntax near '#ret'.
USE BDLab5;
GO
Create Function WasComplaint (#date date, #component varchar)
Returns BIT
Begin
Declare #was int, #ret bit
Select #was = ComponentCode from Complaints
Where ComplaintDate = #date AND
ComponentCode = (Select ComponentCode from Components Where ComponentName = #component)
if (#was = 0)
#ret = 0
else
#ret = 1
Return #ret
End;
I tried different variant of if else syntax but it doesn't help.
You need to use SET when assigning a value to a variable.
USE BDLab5;
GO
Create Function WasComplaint (#date date, #component varchar)
Returns BIT
Begin
Declare #was int, #ret bit
Select #was = ComponentCode from Complaints
Where ComplaintDate = #date AND
ComponentCode = (Select ComponentCode from Components Where ComponentName = #component)
if (#was = 0)
set #ret = 0
else
set #ret = 1
Return #ret
End;
Put the word SET before #ret when you give it a value
Ok here my table - SQL server 2014
Assume that i have the following variables
Computer Hardware > Video Cards
So Computer Hardware is being parent node and Video Cards is being children
Here my stored procedure which is written by programmer logic but not working
Create Procedure insert_Product_Category_With_Parent_Node_Key_pair
(
#cl_CategoryName nvarchar(200),
#cl_ParentCategoryName nvarchar(200),
#cl_InsertedId int Output
)
As
Begin
declare #cl_ParentCategoryId int
declare #cl_Parent_Node_Exists_CategoryId int
set #cl_ParentCategoryId = ISNULL((select cl_CategoryId
from tblProductCategories
where cl_CategoryName=#cl_ParentCategoryName),0)
set #cl_Parent_Node_Exists_CategoryId = ISNULL((select cl_CategoryId
from tblProductCategories
where cl_CategoryName=#cl_CategoryName
and
cl_ParentCategoryId=#cl_ParentCategoryId),0)
CASE
when #cl_Parent_Node_Exists_CategoryId = 0
then
insert into tblProductCategories (cl_CategoryName,cl_ParentCategoryId)
#cl_InsertedId = INSERTED.cl_CategoryId
values (#cl_CategoryName,#cl_ParentCategoryId)
else
#cl_InsertedId = #cl_Parent_Node_Exists_CategoryId
end
end
The error SQL server giving
Msg 156, Level 15, State 1, Procedure insert_Product_Category_With_Parent_Node_Key_pair, Line 17
Incorrect syntax near the keyword 'CASE'.
Msg 102, Level 15, State 1, Procedure insert_Product_Category_With_Parent_Node_Key_pair, Line 22
Incorrect syntax near '#cl_InsertedId'.
Ok here how i want it work
declare #returned_product_categoryId int
exec insert_Product_Category_With_Parent_Node_Key_pair
#cl_CategoryName = 'Video Cards',
#cl_ParentCategoryName = 'Computer Hardware'
#returned_product_categoryId = #cl_InsertedId output
select #returned_product_categoryId
So finally my question how do make this stored procedure work ? My logic is fine however i can't find a way to turn it into SQL logic
When converted to if else logic in that case assignments are giving error
Create Procedure insert_Product_Category_With_Parent_Node_Key_pair
(
#cl_CategoryName nvarchar(200),
#cl_ParentCategoryName nvarchar(200),
#cl_InsertedId int Output
)
AS
BEGIN
SET NOCOUNT ON;
declare #cl_ParentCategoryId int;
declare #cl_Parent_Node_Exists_CategoryId int;
set #cl_ParentCategoryId = ISNULL((select cl_CategoryId
from tblProductCategories
where cl_CategoryName = #cl_ParentCategoryName),0)
set #cl_Parent_Node_Exists_CategoryId = ISNULL((select cl_CategoryId
from tblProductCategories
where cl_CategoryName = #cl_CategoryName
and
cl_ParentCategoryId = #cl_ParentCategoryId),0)
IF (#cl_Parent_Node_Exists_CategoryId = 0)
BEGIN
insert into tblProductCategories (cl_CategoryName,cl_ParentCategoryId)
values (#cl_CategoryName,#cl_ParentCategoryId)
SET #cl_InsertedId = SCOPE_IDENTITY();
END
ELSE
BEGIN
SET #cl_InsertedId = #cl_Parent_Node_Exists_CategoryId;
END
END
You missed set before cl_InsertedId - or u also use Scope_Identity()
Create Procedure insert_Product_Category_With_Parent_Node_Key_pair
(
#cl_CategoryName nvarchar(200),
#cl_ParentCategoryName nvarchar(200),
#cl_InsertedId int Output
)
As
Begin
declare #cl_ParentCategoryId int
declare #cl_Parent_Node_Exists_CategoryId int
set #cl_ParentCategoryId = ISNULL((select cl_CategoryId
from tblProductCategories
where cl_CategoryName=#cl_ParentCategoryName),0)
set #cl_Parent_Node_Exists_CategoryId = ISNULL((select cl_CategoryId
from tblProductCategories
where cl_CategoryName=#cl_CategoryName
and
cl_ParentCategoryId=#cl_ParentCategoryId),0)
if #cl_Parent_Node_Exists_CategoryId = 0
begin
insert into tblProductCategories (cl_CategoryName,cl_ParentCategoryId)
set #cl_InsertedId = INSERTED.cl_CategoryId
values (#cl_CategoryName,#cl_ParentCategoryId)
end
else
begin
set #cl_InsertedId = #cl_Parent_Node_Exists_CategoryId
end
end
I have following stored procedure:
CREATE procedure validateLogin
(
#password varchar(200),
#username varchar(100),
#IpAddress varchar(100)
)
AS
BEGIN
Declare #qry varchar(max), #LockedIp varchar(max), #LockedTime DateTime, #TimeDifference int;
set #qry = 'select IdUser, UserName, FirstName, LastName, idOrg, Users.idRole, Roles.Title as [Role], Allowed_IP from Users, Roles where Users.idRole = Roles.idRole
and lower(UserName) = #username and [password] = #password' ;
select
#LockedIp = isnull(Allowed_IP,''),
#LockedTime = isnull(LockedTime, getDate())
from Users
where UserName = ISNULL(#username,'');
SELECT
#TimeDifference = DATEDIFF(MINUTE, #LockedTime, GETDATE())
IF exists(select * from Users where UserName = #username AND Password = #password AND Active = 1)
BEGIN
IF exists(select * from Users where UserName = #username AND isnull(IsLocked, 0) = 1)
BEGIN -- BE1
IF(#LockedIp = #IpAddress)
BEGIN --BE2
IF (#TimeDifference >5)
BEGIN --BE5
UPDATE Users
SET IsLocked = 0, LockedTime = null
WHERE UserName = ISNULL(#username,'')
exec(#qry);
END --BE5
ELSE
BEGIN
select 'Your Account has been Locked.Try after some time' as Error
END
END --BE2
Else IF(#LockedIp!=#IpAddress)
BEGIN --BE4
UPDATE Users
SET IsLocked = 0, LockedTime = null
WHERE UserName = isnull(#username,'')
exec(#qry);
END --BE4
END -- BE1
Else
BEGIN --BE3
exec(#qry);
END -- BE3
END
END
Go
When I execute this through:
exec validateLogin '|161|217|4|51','admin','127.0.0.1'
I get following error:
Msg 137, Level 15, State 2, Line 3
Must declare the scalar variable "#username".
I have declared this variable in my parameter list, then also error is showing up.
Please help me.
How can I resolve this?
EXEC() will execute in a different scope, so your parameters are not found. You should use sp_executesql and add your parameters that way:
DECLARE #qry NVARCHAR(MAX);
SET #qry = N'select IdUser,UserName,FirstName,LastName,idOrg,Users.idRole,Roles.Title as [Role],Allowed_IP
from Users,Roles
where Users.idRole=Roles.idRole
and lower(UserName)=#username
and [password]=#password' ;
EXECUTE sp_executesql #qry,
N'#username varchar(100), #password varchar(200)',
#Username,
#Password;