Incorrect Syntax When Executing Stored Proc NewId() - sql

IdentityId is of UNIQUEIDENTIFIER type and when I insert a record and set that as NewId(), I get the error message
Incorrect Syntax new )
Not sure what I am missing. Please advice.
Execute command:
Exec dbo.CreateUser
#Title = 'MR',
#FirstName = 'John',
#LastName = 'Smith',
#IdentityId = NewId(),
#PhoneNumber = '01234456789',
#MobileNumber = '0987654321',
#StatesId = 2,
#AddressLineOne = '1 A Road',
#AddressLineTwo = '',
#Town = 'Some Town',
#County = 'County',
#PostCode = 'AA1 1AA',
#Country = 'United Kingdom',
#OrganisationName = 'OrgName',
#OrganisationDomain = 'orgName.com'
CreateUser stored procedure:
CREATE PROCEDURE [dbo].[CreateUser]
#Title NVARCHAR(50),
#FirstName NVARCHAR(50),
#LastName NVARCHAR(50),
#IdentityId UNIQUEIDENTIFIER,
#PhoneNumber NVARCHAR(50),
#MobileNumber NVARCHAR(50),
#StatesId INT,
#AddressLineOne NVARCHAR(MAX),
#AddressLineTwo NVARCHAR(MAX),
#Town NVARCHAR(50),
#County NVARCHAR(50),
#PostCode NVARCHAR(10),
#Country NVARCHAR(20),
#OrganisationName NVARCHAR(MAX),
#OrganisationDomain NVARCHAR(MAX)
AS
INSERT INTO Addresses (AddressLineOne, AddressLineTwo, Town, County, PostCode, Country)
VALUES (#AddressLineOne, #AddressLineTwo, #Town, #County, #PostCode, #Country)
INSERT INTO Organisations(Name, Domain, AddressId)
VALUES (#OrganisationName, #OrganisationDomain, SCOPE_IDENTITY())
INSERT INTO Users (Title, FirstName, LastName, IdentityId, MobileNumber, PhoneNumber, OrganisationId, StatesId, DateCreated)
VALUES (#Title, #FirstName, #LastName, #IdentityId, #PhoneNumber, #MobileNumber, SCOPE_IDENTITY(), #StatesId, GETDATE())

Set the value to a variable and use that:
declare #id uniqueidentifer;
set #id = newid();
exec . . .
#IdentityId = #id,
. . .;
There is nothing specific about newid() here. The exec statement doesn't parse expressions, even simple function calls.
Also, but begin and end around the stored procedure body. You should always do this, to prevent unexpected errors.

Related

Operand Type Clash - SQL Server INSERT stored procedure

I understand the error, I am simply not sure why this is occurring. I have written a simple insert procedure to update a list of users.
Error:
Msg 206, Level 16, State 2, Procedure addNewUser, Line 0 [Batch Start Line 2]
Operand type clash: int is incompatible with date
Procedure declaration:
CREATE PROCEDURE [dbo].[addNewUser]
#fName VARCHAR(255),
#lname VARCHAR(255),
#dob DATE,
#email VARCHAR(255),
#gender VARCHAR(255),
#level VARCHAR(255) AS
INSERT INTO [dbo].[User] ([firstname], [lastname], [dob], [email], [gender], [accesslevel])
VALUES ('#fName ', N'#lname', #dob, N'#email', N'#gender', N'#level')
Calling procedure:
DECLARE #return_value int
EXEC #return_value = [dbo].[addNewUser]
#fName = N'Ste',
#lname = N'King',
#dob = 19780103,
#email = N'Books#email.com',
#gender = N'Male',
#level = N'Free'
SELECT 'Return Value' = #return_value
GO
Just put #dob = 19780103 into quotes '1978-01-03':
USE [t7068097]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[addNewUser]
#fName = N'Ste',
#lname = N'King',
#dob = '1978-01-03',
#email = N'Books#email.com',
#gender = N'Male',
#level = N'Free'
SELECT 'Return Value' = #return_value
GO
UPDATE:
SELECT CAST('1978-1-1' AS DATE)
SELECT CAST('19780101' AS DATE)
First your procedure is wrong. You shouldn't put your variables into the single quote for your purpose:
ALTER PROCEDURE [dbo].[addNewUser]
#fName varchar(255),
#lname varchar(255),
#dob DATE,
#email varchar(255),
#gender varchar(255),
#level varchar(255)
AS
INSERT [dbo].[User]
(
[firstname]
, [lastname]
, [dob]
, [email]
, [gender]
, [accesslevel]
)
VALUES
(
#fName
, #lname
, #dob
, #email
, #gender
, #level
)
GO
Also you send int value to the date field. You need yo put into the single quote:
DECLARE #return_value int
EXEC #return_value = [dbo].[addNewUser]
#fName = N'Ste',
#lname = N'King',
#dob = '19780103',
#email = N'Books#email.com',
#gender = N'Male',
#level = N'Free'
SELECT 'Return Value' = #return_value

Query works but stored procedure gives me a formatting Error

Query returns exactly what I want but when I plug the same structure into my stored Procedure I get a formatting error.
Msg 8116, Level 16, State 1, Procedure RegisterUser, Line 21
Argument data type varchar is invalid for argument 1 of format function.
Here's my query.
DECLARE #Department int
SET #Department = (SELECT DepartmentLink FROM DepartmentMaster WHERE Name =
'Inventory')
DECLARE #Position int
SET #Position = (SELECT TypeLink FROM EmployeeTypes Where Title = 'Clerk')
DECLARE #UID int
set #UID = (SELECT ID FROM EmployeeMaster WHERE FirstName = 'John' and
LastName = 'Doe')
(SELECT convert(varchar,FORMAT (#Department,'00#'))+'-'+
convert(varchar,FORMAT(#Position, '0#'))+'-
'+convert(varchar,FORMAT(#UID,'000#')) as EmployeeID)
Here's my Stored Procedure..
CREATE Procedure RegisterUser
(
#FirstName varchar(255),
#LastName varchar(255),
#Department varchar(255),
#Email varchar(255),
#Password varchar(255),
#UserType varchar(255),
#EmployeeID varchar(255),
#DepartmentLink int,
#Position int,
#UID int
)
AS
SET #Department = (SELECT DepartmentLink FROM DepartmentMaster WHERE Name =
#Department)
SET #Position = (SELECT TypeLink FROM EmployeeTypes Where Title = #UserType)
set #UID = (SELECT ID FROM EmployeeMaster WHERE FirstName = #FirstName and
LastName = #LastName)
SET #EmployeeID = (SELECT convert(varchar,FORMAT (#Department,'00#'))+'-'+
convert(varchar,FORMAT(#Position, '0#'))+'-
'+convert(varchar,FORMAT(#UID,'000#')) as EmployeeID)
INSERT INTO EmployeeMaster(FirstName, LastName, Department,
Email,Password,CreationDate, EmployeeID, Active)
VALUES(#FirstName, #LastName, (SELECT DepartmentLink FROM DepartmentMaster
WHERE Name = #Department), #Email, #Password, GETDATE(),
#EmployeeID
,1)
I would suggest writing this as:
CREATE Procedure RegisterUser (
#FirstName varchar(255),
#LastName varchar(255),
#Department varchar(255),
#Email varchar(255),
#Password varchar(255),
#UserType varchar(255),
#EmployeeID varchar(255),
#DepartmentLink int,
#Position int,
#UID int
) AS
BEGIN
DECLARE #DepartmentLink varchar(255);
DECLARE #PositionLink int;
DECLARE #Uid_new int;
DECLARE #EmployeeId_new varchar(255);
SELECT #DepartmentLink = DepartmentLink
FROM DepartmentMaster
WHERE Name = #Department;
SELECT #PostiionLink = TypeLink
FROM EmployeeTypes
WHERE Title = #UserType;
SELECT #Uid_new = ID
FROM EmployeeMaster
WHERE FirstName = #FirstName and LastName = #LastName;
SELECT #EmployeeID_new = (FORMAT(#Department, '00#') + '-' +
FORMAT(#Position, '0#')) + '-' +
FORMAT(#UID, '000#')
);
INSERT INTO EmployeeMaster(FirstName, LastName, Department, Email, Password, CreationDate, EmployeeID, Active)
VALUES(#FirstName, #LastName,
#DepartmentLink, #Email, #Password, GETDATE(),
#EmployeeID_new, 1
);
END; -- RegisterUser
You could simplify this further.
Notes:
Your code needs a BEGIN/END
Don't assign values to parameters, unless they are output parameters. Instead, use local variables.
FORMAT() already returning a string -- no need for another conversion.
SET with a subquery just looks awkward to me, when you can do the assignment with a SELECT.
I don't think you need all the arguments, but that is another matter.

Procedure or function has too many arguments SQL server

I getting this error while executing the stored procedure in SQL Server:
Msg 8144, Level 16, State 2, Procedure sp_adduser, Line 2
Procedure or function sp_adduser has too many arguments specified.
The weird thing is that the number of parameters is the same as declared in the procedure, even when I execute it while right-click on the procedure and press execute, input the parameters from the fields and still the same error.
This is the code of the procedure and the exec:
create procedure [dbo].[sp_addUser]
(
#userID nvarchar(50),
#pw nvarchar(50),
#fName nvarchar(50),
#lname nvarchar(50),
#email nvarchar(150),
#address nvarchar(150),
#city int,
#country int,
#phone nvarchar(50),
#gender nvarchar(10),
#dob date,
#photo nvarchar(150),
#secq int,
#secAnswer nvarchar(150),
#completed int output)
as
declare #found int
/*
usertype:
0 - Administrator
1 - User
*/
begin
begin
select #found=count(*) from registration r
where REPLACE(r.firstname,' ','')=REPLACE(#fName,' ','')
and REPLACE(r.lastname,' ','')=REPLACE(#lname,' ','')
and r.dateofbirth=#dob;
end
begin
if #found=0
begin
begin
insert into Login values(#userID,#pw,0,1);
end
begin
insert into registration values(
#userID,#fName,#lname,#email,
#address,#city,#country,#phone,
#gender,#dob,#photo,#secq,#secAnswer
)
end
begin
set #completed=1;
end
end
else set #completed=0
end
return #completed
end
And here is the exec:
DECLARE #return_value int,
#completed int
SELECT #completed = 0
EXEC #return_value = [dbo].[sp_addUser]
#userID = N'a',
#pw = N'a',
#fName = N'a',
#lname = N'a',
#email = N'a',
#address = N'a',
#city = 1,
#country = 1,
#phone = N'a',
#gender = N'a',
#dob = '01/01/2000',
#photo = N'a',
#secq = 1,
#secAnswer = N'a',
#completed = #completed OUTPUT
SELECT #completed as N'#completed'
SELECT 'Return Value' = #return_value
Thanks in advance!
sp_adduser is a procedure which is built-in to SQL Server - see https://msdn.microsoft.com/en-us/library/ms181422.aspx
Don't use the sp_ prefix for your own stored procedures, maybe call it proc_adduser instead, I think that will work
You are actually calling the system stored procedure sp_adduser. The "sp_" prefix is special and has its own rules on resolution, so using it is recommended against. If you want to use your own prefix, whatever you do, don't use "sp_".
Per #marc_s, even if your stored procedure doesn't clash with an existing system stored procedure, you will still get a noticeable performance hit due to cache misses.

SQL Trigger not firing

I Update the table and the Trigger doesn't fire. Or at least it doesn't seem to insert into the other table the information. Is there something I may be missing in my code?
ALTER TRIGGER [dbo].[UpdateUsers]
ON [VLS_TEST].[dbo].[Identifier]
AFTER UPDATE
AS
DECLARE #Status VARCHAR(4)
DECLARE #License_Nbr VARCHAR(9)
DECLARE #Email VARCHAR (100)
DECLARE #FName VARCHAR(15)
DECLARE #LName VARCHAR(25)
SELECT #Status = Status,
#License_Nbr = License_Nbr,
#Email = Email,
#FName = FName,
#LName = LName
FROM VLS_Test.dbo.Identifier
IF ( #Status = 'LISC'
AND ( #License_Nbr LIKE '__01%'
OR #License_Nbr LIKE '__06%' ) )
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF NOT EXISTS (SELECT *
FROM RTT_DEV.dbo.Users
WHERE RTT_DEV.dbo.Users.TerminalOperatorNum = #License_Nbr)
BEGIN
INSERT INTO RTT_DEV.dbo.Users
(Username,
PASSWORD,
UserType,
TerminalOperatorNum,
AdminName,
FirstName,
LastName)
VALUES (#Email,
'********',
'2',
#License_Nbr,
'System',
#FName,
#LName)
END
END

SQL use Unicode N in a Stored Procedure with variable

I have the following Stored Procedure, Im looking for the correct syntax so I can play the Comments Value in the Comments column with N in front end the value for Unicode I need save Russian Characters Values
So at the moment the comments value is being passed as such
#comments
I want to do
N#comments but not working
ALTER PROCEDURE [dbo].[spInsertContactUs]
(
#title VARCHAR(20) = default,
#forename VARCHAR(100) = default,
#surname VARCHAR(100) = default,
#gender VARCHAR(50) = default,
#address1 VARCHAR(100) = default,
#address2 VARCHAR(100) = default,
#city VARCHAR(50) = default,
#county VARCHAR(50) = default,
#country INT = default,
#zipcode VARCHAR(50) = default,
#email VARCHAR(200) = default,
#comments NVARCHAR(MAX) = default,
#mailinglist BIT = default,
#address3 VARCHAR(100) = default,
#dateOfBirth datetime = default
)
AS
SET NOCOUNT ON
INSERT INTO tblContactUs (
dateAdded,
title,
forename,
surname,
gender,
address1,
address2,
city,
county,
country,
zipcode,
email,
comments,
mailinglist,
address3,
dateOfBirth)
VALUES (
getdate(),
#title,
#forename,
#surname,
#gender,
#address1,
#address2,
#city,
#county,
#country,
#zipcode,
#email,
#comments,
#mailinglist,
#address3,
#dateOfBirth
)
SET NOCOUNT OFF
RETURN
;
Use Nvarchar data type in table's fields and stored procs parameters.
ADDED
See this link, maybe this will help you.
This works. I figured it out myself:
Command.Parameters.Add("#prc", SqlDbType.NVarChar).Value = strPrc;
You need to change your SqlDbType into NVarChar. You also need to change the field into Nvarchar field and the stored procedure into that too.
declare #a nvarchar(50)
set #a =N'تست'
exec('INSERT INTO [dbo].[tblP] ([productVal],[name],[month]) VALUES ( 9, '+'N'''+#a+''',1)')
INSERT INTO [dbo].[tblP] ([productVal],[year],[month]) VALUES ( 9, N'تست',1)