Query:
UPDATE EMPLOYEE AS E
INNER JOIN EMPLOYEE_TEL AS T ON E.EMP_NUMBER = T.EMP_NUMBER
SET E.FIRST_NAME = #fname
,E.MID_NAME = #mname
,E.INITIALS =#initilas
,E.SURNAME = #sname
,E.GENDER = #gender
,E.CIVIL_STATUS = #CS
,E.DOB =#datetime
,E.NIC_NUMBER = #nic
,E.ADDRESS_LINE1 =#adline1
,E.ADDRESS_LINE2 = #adline2
,E.ADDRESS_LINE3 = #adline3
,E.EMAIL = #email
,E.DESG_NO =#designo
,E.BASIC_SALARY = #sal
,E.TITLE = #title
,T.TELEPHONE=#tel
WHERE E.EMP_NUMBER=#empnum
I have tried in this SQL Server, but it came up with an error
'Msg 156, Level 15, State 1, Procedure SPUPDATEEMP, Line 21
Incorrect syntax near the keyword 'AS'.
I can't find the error. Is this wrong?
UPDATE EMPLLOYEE
SET FIRST_NAME = #fname
,MID_NAME = #mname
,INITIALS =#initilas
,SURNAME = #sname
,GENDER = #gender
,CIVIL_STATUS = #CS
,DOB =#datetime
,NIC_NUMBER = #nic
,ADDRESS_LINE1 =#adline1
,ADDRESS_LINE2 = #adline2
,ADDRESS_LINE3 = #adline3
,EMAIL = #email
,DESG_NO =#designo
,BASIC_SALARY = #sal
,TITLE = #title
WHERE EMP_NUMBER=#empnum
UPDATE EMPLOYEE_TEL
SET TELEPHONE=#tel
WHERE EMP_NUMBER=#empnum
When doing JOINS you don't need to use "as" to alias a table name.
However, when doing UPDATES you can't alias the name like you are trying to do.
UPDATE E
FROM Employee
What you're trying to do is update two tables at once - you cannot do this in SQL Server - you'll have to split this up into two separate UPDATES:
UPDATE EMPLOYEE
SET
FIRST_NAME = #fname,
MID_NAME = #mname,
.....
TITLE = #title
WHERE
EMP_NUMBER = #empnum
UPDATE EMPLOYEE_TEL
SET
TELEPHONE = #tel
WHERE
EMP_NUMBER = #empnum
Related
I want to force a user's string input in a stored procedure to uppercase. I tried writing UPPER prior to the #parameterName but I got a syntax error. Is this possible? Would be it be better suited to convert the string to uppercase in the statement itself? Here's the code to my SP where I was attempting to use UPPER in the parameter definition.
ALTER PROCEDURE [dbo].[UpdateEntries]
UPPER #ENTRY_TYPE NVARCHAR(20) = '',
UPPER #ENTRY_NAME NVARCHAR(50),
#CLASS_TYPE_ID INT,
#ENTRY_PRICE DEC(4,2),
#ENTRY_DESCRIPT NVARCHAR(max),
#PET_FRIENDLY BIT,
#AGE_RESTRICTION BIT,
#PRICE_RANGE_ID INT,
#RESTAURANT_TYPE_ID INT NULL
AS
BEGIN
SET NOCOUNT ON;
IF #ENTRY_TYPE = 'ACTIVITY'
BEGIN
UPDATE ACTIVITY_DETAIL
SET ACT_NAME = #ENTRY_NAME,
ACT_PRICE = #ENTRY_PRICE,
ACT_DESCRIPT = #ENTRY_DESCRIPT,
ACT_DOG_FRIENDLY = #PET_FRIENDLY,
ACT_AGE_RESTRICTION = #AGE_RESTRICTION,
ACT_PRICE_RANGE_ID = #PRICE_RANGE_ID
WHERE NOT EXISTS (SELECT 1 FROM dbo.[ACTIVITY_DETAIL] WHERE ACT_NAME = #ENTRY_NAME);
END
IF #ENTRY_TYPE = 'BUSINESS'
BEGIN
UPDATE BUSINESS_DETAIL
SET BUSINESS_NAME = #ENTRY_NAME,
BUSINESS_PRICE = #ENTRY_PRICE,
BUSINESS_DESCRIPT = #ENTRY_DESCRIPT,
BUSINESS_DOG_FRIENDLY = #PET_FRIENDLY,
BUSINESS_PRICE_RANGE_ID = #PRICE_RANGE_ID
WHERE NOT EXISTS (SELECT 1 FROM dbo.[BUSINESS_DETAIL] WHERE BUSINESS_NAME = #ENTRY_NAME);
END
IF #ENTRY_TYPE = 'HOTEL'
BEGIN
UPDATE HOTEL_DETAIL
SET HOTEL_NAME = #ENTRY_NAME,
HOTEL_PRICE = #ENTRY_PRICE,
HOTEL_DESCRIPT = #ENTRY_DESCRIPT,
HOTEL_PET_FRIENDLY = #PET_FRIENDLY,
HOTEL_PRICE_RANGE_ID = #PRICE_RANGE_ID
WHERE NOT EXISTS (SELECT 1 FROM dbo.[HOTEL_DETAIL] WHERE HOTEL_NAME = #ENTRY_NAME);
END
IF #ENTRY_TYPE = 'RESTAURANT'
BEGIN
UPDATE RESTAURANT_DETAIL
SET RESTAURANT_NAME = #ENTRY_NAME,
RESTAURANT_PRICE_AVG = #ENTRY_PRICE,
RESTAURANT_DESCRIPT = #ENTRY_DESCRIPT,
RESTAURANT_DOG_FRIENDLY = #PET_FRIENDLY,
RESTAURANT_PRICE_RANGE_ID = #PRICE_RANGE_ID
WHERE NOT EXISTS (SELECT 1 FROM dbo.[RESTAURANT_DETAIL] WHERE RESTAURANT_NAME = #ENTRY_NAME);
END
END
UPPER #ENTRY_TYPE NVARCHAR(20) = '',
UPPER #ENTRY_NAME NVARCHAR(50),
These are wrong. No such usage.
#ENTRY_TYPE NVARCHAR(20) = '',
#ENTRY_NAME NVARCHAR(50),
SELECT #ENTRY_TYPE = UPPER(#ENTRY_TYPE);
SELECT #ENTRY_NAME = UPPER(#ENTITY_NAME);
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 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;
I'm writing a stored procedure and it's working for if but not for else. For if I'm getting the correct ID value but for else it's just giving me null.
SELECT #ID = ID FROM PRODUCTS WHERE SN = #SN
SELECT #Chip_ID = Chip_ID FROM PRODUCTS WHERE SN = #SN
SELECT #Power = Power From Module_Cycle WHERE ChipID = #Chip_ID
SELECT #Test_ID=Test_ID FROM TestEE_Mod WHERE ID=#ID AND #TypeID = TypeID
IF(#Test_ID IS NOT NULL)
BEGIN
IF(#TypeID = '3')
BEGIN
SELECT #Temp_TestID=TestID FROM TempCycle WHERE ChipID = #Chip_ID AND #Power = 'false'
BEGIN
UPDATE TestEE_Mod SET Temp_TestID = #Temp_TestID WHERE ID = #ID AND TypeID = #TypeID
END
END
ELSE
BEGIN
SELECT #Temp_TestID=TestID FROM TempCycle WHERE ChipID = #Chip_ID AND #Power = 'true'
BEGIN
UPDATE TestEE_Mod SET Temp_TestID = #Temp_TestID WHERE ID = #ID AND TypeID = #TypeID
END
END
END
Most likely the issue lies in the #Power variable. In your two SELECT statements, the only difference is that one includes #Power = 'false' in the WHERE clause, while the other includes #Power = 'true'.
Since #Power is a variable, not an actual column in your TempCycle table, I'm guessing you actually meant to filter by Power = #Power in both of them.
This stored procedure is use for searching records.
When I only fill in the parameter #ID or #FirstName, it works. But not if I only fill in #LastName.
For example:
#ID = 1, the rest is NULL --> should give 1 row --> RESULT: 1 row (ok)
#ID = NULL, #FirstName = 'Tim', the rest is NULL --> should give 1 row --> RESULT: 1 row (ok)
#ID = NULL, #FirstName = NULL, #LastName = 'BlaBla', the rest is NULL
--> should give 1 row --> RESULT: all rows (Not OK)
Anyone know why?
Thanks in advance.
This is my procedure:
ALTER PROCEDURE lookupSubscriber
-- Add the parameters for the stored procedure here
#ID int,
#firstname nvarchar(50),
#lastname nvarchar(60),
#street nvarchar(80),
#housenumber nvarchar(6),
#companyname nvarchar(50),
#city nvarchar(50),
#ResultString nvarchar(80) OUTPUT,
#ResultValue int OUTPUT,
#ResultCount int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--Replacing empty strings with NULL
IF #ID = 0 BEGIN SET #ID = NULL; END
IF #firstname = '' BEGIN SET #firstname = NULL; END
IF #lastname = '' BEGIN SET #lastname = NULL; END
IF #street = '' BEGIN SET #street = NULL; END
IF #companyname = '' BEGIN SET #companyname = NULL; END
IF #housenumber = '' BEGIN SET #housenumber = NULL; END
IF #city = '' BEGIN SET #city = NULL; END
-- Insert statements for procedure here
BEGIN TRY
SELECT s.ID, COALESCE(d.FirstName,'NONE'), COALESCE(d.LastName,'NONE'), d.Street, COALESCE(d.CompanyName,'NONE'), d.HouseNumber, c.name
FROM Subscriber s
INNER JOIN SubscriberDetail d ON d.ID = s.Detail_ID
INNER JOIN City c ON d.City_ID = c.ID
WHERE (s.ID = COALESCE(#ID, s.ID)
AND d.FirstName = COALESCE(#firstname, d.FirstName) OR d.FirstName = 'NONE'
AND d.LastName = COALESCE(#lastname, d.LastName) OR d.LastName = 'NONE'
AND d.Street = COALESCE(#street, d.Street)
AND d.CompanyName = COALESCE(#companyname, d.CompanyName) OR d.CompanyName = 'NONE'
AND d.HouseNumber = COALESCE(#housenumber, d.HouseNumber)
AND c.name = COALESCE(#city, c.name))
SET #ResultCount = ##ROWCOUNT
SET #ResultString = 'Lookup successful'
SET #ResultValue = 0
END TRY
BEGIN CATCH
SET #ResultString = 'ERROR: ' + ERROR_MESSAGE()
SET #ResultValue = 2
END CATCH
END
GO
Example data:
Subscriber:
ID = 1 | Type_ID = 1 | Detail_ID = 2
ID = 2 | Type_ID = 2 | Detail_ID = 3
SubscriberDetail:
ID = 1 | FirstName = 'Laurens' | LastName = 'De Neys' | CompanyName = NULL | Street = 'Ergens' | HouseNumber = 2 | City_ID = 1
ID = 2 | FirstName = 'Tim' | LastName = 'Blabla' | CompanyName = NULL | Street = 'Iets' | HouseNumber = 26 | City_ID = 2
City:
ID = 1 | name = 'Liedekerke' | postalCode = 1770
ID = 1 | name = 'Leuven' | postalCode = 3000
Perhaps you need to put your OR conditions in parentheses with your original conditions?
For all of these expressions:
AND d.LastName = COALESCE(#lastname, d.LastName) OR d.LastName = 'NONE'
Change to
AND (d.LastName = COALESCE(#lastname, d.LastName) OR d.LastName = 'NONE')
EDIT
Well, I can't know what is in your DB, but I have a feeling some of that logic is incorrect. Try this:
SELECT s.ID, COALESCE(d.FirstName,'NONE'), COALESCE(d.LastName,'NONE'), d.Street, COALESCE(d.CompanyName,'NONE'), d.HouseNumber, c.name
FROM Subscriber s
INNER JOIN SubscriberDetail d ON d.ID = s.Detail_ID
INNER JOIN City c ON d.City_ID = c.ID
WHERE (#id is null or s.ID = #ID)
AND (#firstname is null or d.FirstName = #firstname)
AND (#lastname is null or d.LastName = #lastname)
AND (#street is null or d.Street = #street)
AND (#companyname is null or d.CompanyName = #companyname)
AND (#housenumber is null or d.HouseNumber = #housenumber)
AND (#city is null or c.name = #city)
Your WHERE statement has its AND and OR conditions arranged in such a way that they are evaluating incorrectly for what I assume you want them to do. You can fix it by encapsulating your OR statments to account for the correct parts. Example:
WHERE (s.ID = COALESCE(#ID, s.ID)
AND (d.FirstName = COALESCE(#firstname, d.FirstName) OR d.FirstName = 'NONE')
AND (d.LastName = COALESCE(#lastname, d.LastName) OR d.LastName = 'NONE')
AND d.Street = COALESCE(#street, d.Street)
AND (d.CompanyName = COALESCE(#companyname, d.CompanyName) OR d.CompanyName = 'NONE')
AND d.HouseNumber = COALESCE(#housenumber, d.HouseNumber)
AND c.name = COALESCE(#city, c.name))
EDIT: Looking at the same data, there's an additional problem here. You're trying to check equality on Company name to COALESCE(#companyname, d.companyname), but you have cases where your CompanyName is NULL. When a value is NULL, it's unknown, so SQL won't treat it as being equal, even to itself.
This is one reason I generally prefer, rather than the COALESCE syntax above to do somethng like this:
AND (#companyname IS NULL OR d.CompanyName = #companyname)
The above doesn't care if the stored value for company name is null if the parameter is also null (and if the parameter isn't null, your equality still works out).