Stored procedure with OUTPUT - Must declare the scalar variable - sql

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

Related

SQL Server Dynamic where clause query

I am trying to create a dynamic query I just to do with linq
from a in Customers
where (string.IsNullOrEmpty(name)? true : a.FirstName == name) && (string.IsNullOrEmpty(last)? true : a.LastName == last)
select a;
but now I need to do in on Stored Procedure and I don't want to concatenate for security reason and performance. The most close example I found is this query
declare #Name as varchar(100)
declare #GroupName as varchar(100)
set #Name = ''
set #GroupName = 'Manufacturing'
SELECT TOP (1000) [DepartmentID]
,[Name]
,[GroupName]
,[ModifiedDate]
FROM [AdventureWorks2017].[HumanResources].[Department]
where ([Name] = case
when #Name is null or #Name = '' then null
else #Name
end
)
and
(
[GroupName] = case
when #GroupName is null or #GroupName = '' then null
else #GroupName
end
)
This almost works. I think this should be the answer but the where clause fails for obvious reason.
I would like the where clause could produce '1=1' if the param "Name" or "GroupName" is null
example
#Name = "Somename"
#GroupName = null
where (Name = #Name) and (1 = 1)
--or
#Name = null
#GroupName = "Somegruopname"
where (1 = 1) and (GroupName = #GroupName)
--
#Name = null
#GroupName = null
where (1 = 1) and (1 = 1)
You want it to succeed if the variable is null or empty or a match, so I would just write that in your stored procedure.
WHERE (#FirstName is null OR #FirstName = '' OR [FirstName] = #FirstName)
AND (#LastName is null OR #LastName = '' OR [LastName] = #LastName)
Please try this:
declare #Name as varchar(100)
declare #GroupName as varchar(100) = 'Manufacturing'
set #Name = LTRIM(RTRIM(#Name))
set #GroupName = 'Manufacturing'
SELECT TOP (1000) [DepartmentID]
,[Name]
,[GroupName]
,[ModifiedDate]
FROM [AdventureWorks2017].[HumanResources].[Department]
where ([Name] = coalesce(#Name,'') = '' OR [Name] = #Name)
and
([GroupName] = coalesce(#GroupName, '') = '' OR [GroupName] = #GroupName)

Sql Scope_Identity() return Null?

I am facing this issue that my stored proc always return NULL though i have set my #output variable as well.I want to get last inserted scope Id from the table.
can someone help me where i have got wrong?
ALTER PROC spAddOrUpdateMember
#pMemberId INT = 0 ,
#pFirstName VARCHAR(50) = 'aa',
#pLastName VARCHAR(50)='aa' ,
#pMemberCode VARCHAR(15) = '12312',
#pDOB DATE = '03/10/2019',
#pGrade INT = 2 ,
#pCNIC VARCHAR(14) = '3423434',
#pFatherName VARCHAR(50) = 'asdasd' ,
#pCurrentAddress VARCHAR(MAX) = 'asds' ,
#pPermanentAddress VARCHAR(MAX) = 'fgdf',
#pEmploymentAddress VARCHAR(MAX) = 'ytuyu' ,
#pNationality INT =2
#output int = 0 output
AS
BEGIN
IF #pMemberId > 0
BEGIN
---UPDATE ME
UPDATE [dbo].[QC_Member_Profile]
SET
[FirstName] = #pFirstName
,[LastName] = #pLastName
,[DOB] = #pDOB
,[CNIC] = #pCNIC
,[FatherName] = #pFatherName
,[CurrentAddress] = #pCurrentAddress
,[PermanentAddress] = #pPermanentAddress
,[Nationality] = #pNationality
,[MemberTypeId] =#pMemberTypeId
WHERE MemberId = #pMemberId
END
ELSE
BEGIN
---INSERT ME
INSERT INTO QC_Member_Profile VALUES(
dbo.PIdentityKey(0),
#pFirstName,
#pLastName,
#pDOB,
#pCNIC,
#pFatherName,
#pCurrentAddress,
#pPermanentAddress,
#pNationality,
)
set #output = SCOPE_IDENTITY();
SELECT #output = SCOPE_IDENTITY();
select #output
END
END
I've guessed the name of your ID column, however, this should work. You'll need to amend the name of your ID column if it isn't called MemberID or if it doesn't have the data type int:
ALTER PROC spAddOrUpdateMember #pMemberId int = 0,
#pFirstName varchar(50) = 'aa',
#pLastName varchar(50) = 'aa',
#pMemberCode varchar(15) = '12312',
#pDOB date = '03/10/2019',
#pGrade int = 2,
#pCNIC varchar(14) = '3423434',
#pFatherName varchar(50) = 'asdasd',
#pCurrentAddress varchar(MAX) = 'asds',
#pPermanentAddress varchar(MAX) = 'fgdf',
#pEmploymentAddress varchar(MAX) = 'ytuyu',
#pNationality int = 2,
#output int = 0 OUTPUT
AS
BEGIN
IF #pMemberId > 0
BEGIN
UPDATE [dbo].[QC_Member_Profile]
SET [FirstName] = #pFirstName,
[LastName] = #pLastName,
[DOB] = #pDOB,
[CNIC] = #pCNIC,
[FatherName] = #pFatherName,
[CurrentAddress] = #pCurrentAddress,
[PermanentAddress] = #pPermanentAddress,
[Nationality] = #pNationality,
[MemberTypeId] = #pMemberTypeId
WHERE MemberId = #pMemberId;
END;
ELSE
BEGIN
DECLARE #ins table (OutputID int);
INSERT INTO QC_Member_Profile
OUTPUT Inserted.MemberID --guessed name
INTO #Ins
VALUES (dbo.PIdentityKey(0), #pFirstName, #pLastName, #pDOB, #pCNIC, #pFatherName, #pCurrentAddress, #pPermanentAddress, #pNationality);
SELECT #output = OutputID
FROM #ins;
SELECT #Output;
END;
END;
I've also fixed the syntax errors that were in your original question.

SQL Server 2008 R2: Prepare Dynamic WHERE Clause

I have the following stored procedure with four parameters.
Stored procedure spTest:
CREATE PROCEDURE spTest
#Name varchar(20) = '',
#Address varchar(100) = '',
#City varchar(50) = '',
#Pin varchar(50) = ''
AS
DECLARE #DynamicWhere varchar(max)
DECLARE #Query varchar(max)
/* Here I want to prepare a dynamic where clause for all possibilities */
SET #Query = 'SELECT * FROM Test_Table '+ #DynamicWhere +'';
EXECUTE(#Query);
GO
Well I am preparing it like this:
IF #Name = '' AND #Address = '' AND #City = '' AND #Pin = ''
BEGIN
SET #DynamicWhere = '';
END
ELSE IF #Name != '' AND #Address = '' AND #City = '' AND #Pin = ''
BEGIN
SET #DynamicWhere = 'Name ='''+#Name+'''';
END
ELSE IF #Name != '' AND #Address != '' AND #City = '' AND #Pin = ''
BEGIN
SET #DynamicWhere = 'Name ='''+#Name+''' AND Address ='''+#Address+'''';
END
......
......
Many possibilities
Is this a right way OR is there any better way to prepare the dynamic WHERE clause?
It's called catch-all queries and it basically goes like this:
CREATE PROCEDURE spTest
#Name varchar(20) = '',
#Address varchar(100) = '',
#City varchar(50) = '',
#Pin varchar(50) = ''
AS
SELECT *
FROM Test_Table
WHERE (#Name = '' OR Name = #Name)
AND (#Address = '' OR Address = #Address)
AND (#City = '' OR City = #City)
AND (#Pin = '' OR Pin = #Pin);
GO
You also might want to read this article about catch all queries
You can use ISNULL and NULLIF also in this case:
below code should work :
CREATE PROCEDURE spTest
#Name varchar(20) = '',
#Address varchar(100) = '',
#City varchar(50) = '',
#Pin varchar(50) = ''
AS
SET #Name=NULLIF(#Name,'')
SET #Address=NULLIF(#Address,'')
SET #City=NULLIF(#City,'')
SET #Pin=NULLIF(#Pin,'')
SELECT *
FROM Test_Table
WHERE Name = ISNULL(#Name,Name)
AND Address = ISNULL(#Address,Address)
AND City = ISNULL(#City,City)
AND Pin = ISNULL(#Pin,Pin)
GO
I update the #Zohar answer. Define blank is bad habits, ideally define with null and later use blank. So the query will be
CREATE PROCEDURE spTest
#Name varchar(20) = null,
#Address varchar(100) = null,
#City varchar(50) = null,
#Pin varchar(50) = null
AS
SELECT *
FROM Test_Table
WHERE (Name = ISNULL(#Name,'') )
AND (Address = ISNULL(#Address,''))
AND (City = ISNULL(#City,''))
AND (Pin = ISNULL(#Pin,''));
GO
Even I like the #Biswa answer as it use current version of sql server, but Sqlserver 2008R2 does not have this function.

Insert Stored Procedure with null parameters

I want to create an insert stored procedure with null parameters, if pass the value for that parameter then it has to insert or update in database
My stored procedure is:
Create proc [dbo].[SP_InsertOrUpdateCourseDetails]
#CourseID int,
#Tab1Title nvarchar(250) = null,
#Tab1Description nvarchar(max) = null,
#Tab2Title nvarchar(250) = null,
#Tab2Description nvarchar(max) = null,
#Tab3Title nvarchar(250) = null,
#Tab3Description nvarchar(max) = null,
#Tab4Title nvarchar(250) = null,
#Tab4Description nvarchar(max) = null,
#Syllabus nvarchar(max) = null
As
Begin
If NOT EXISTS (Select * from CourseDetail Where CourseID=#CourseID )
Begin
Insert into CourseDetail(CourseID, Tab1Title, Tab1Description,
Tab2Title, Tab2Description,
Tab3Title, Tab3Description, Tab4Title, Tab4Description,
Syllabus)
values (#CourseID, #Tab1Title, #Tab1Description, #Tab2Title, #Tab2Description,
#Tab3Title, #Tab3Description, #Tab4Title, #Tab4Description, #Syllabus)
IF ##ERROR = 0 AND ##ROWCOUNT =1
Begin
Select top 1 CourseID from CourseDetail Order by CourseDetailID Desc
End
Else
Begin
Select 0
End
End
Else
Begin
Update CourseDetail
SET
Tab1Title = #Tab1Title,
Tab1Description = #Tab1Description,
Tab2Title = #Tab2Title,
Tab2Description = #Tab2Description,
Tab3Title = #Tab3Title,
Tab3Description = #Tab3Description,
Tab4Title = #Tab4Title,
Tab4Description = #Tab4Description,
Syllabus = #Syllabus
Where
CourseID = #CourseID
IF ##ERROR = 0 AND ##ROWCOUNT =1
Begin
Select top 1 CourseID from CourseDetail Order by CourseDetailID Desc
End
Else
Begin
Select 0
End
End
End
You can use ISNULL function passing in it SP's parameter as first parameter and default value in case of insert or actual column's value in case of update:
Insert into CourseDetail(CourseID, Tab1Title, Tab1Description,
Tab2Title, Tab2Description,
Tab3Title, Tab3Description, Tab4Title, Tab4Description,
Syllabus)
values (#CourseID, ISNULL(#Tab1Title, 'default value'), ISNULL(#Tab1Description,'default value'), ...
Update CourseDetail
SET
Tab1Title = ISNULL(#Tab1Title, Tab1Title),
Tab1Description = ISNULL(#Tab1Description, Tab1Description),
....
Where
CourseID = #CourseID
But in this case you won't can to set NULL value explicitely even if you'll need

Dont want Select variable displaying values

It might be simple but I been having trouble figuring out.
I have a piece of code(Similar to Below Code) where I am assigning bunch of values to bunch of variables via select, it does the job but when I am running it I am getting result set which is causing my SSMS to crash is there way to avoid this....
DECLARE #Name VARCHAR(100)
DECLARE #LastName VARCHAR(100)
DECLARE #Address VARCHAR(100)
SELECT TOP 1
#Name = Name
#LastName = LastName
#Address = Address
From Person.Address
Where Name = 'Name'
Order By ID
I am using the above code in a loop where I am processing around 3-400K rows and pass this variables to a stored procedure, each top 1 select statement throws a result set causing my SSMS to crash I dont really need the select top 1 values to be displayed, any idea how to get rid of this?....
Any help would be much appreciated.
---As requested below is the code, I have hashed few things but this is what it is and I am running it from Query Analayzer as this is only 1 time process so we dont need SP to be created.
DECLARE #retstat INT
DECLARE #Name VARCHAR(255)
DECLARE #Lang TINYINT
DECLARE #Address CHAR(10)
DECLARE #ID INT
DECLARE #BranchSeqNo INT
DECLARE #AddressCode VARCHAR(10)
DECLARE #Address1 VARCHAR(50)
DECLARE #City VARCHAR(30)
DECLARE #State VARCHAR(3)
DECLARE #PostalCode VARCHAR(15)
DECLARE #Country VARCHAR(30)
SET #ID = NULL
UPDATE RAWClaimTable Set Processed = 0 where Processed is null
UPDATE RAWClaimTable SET ErrorCode = NULL ,ErrorMessage = NULL ,Processed = 0
WHERE ErrorMessage IS NOT NULL AND CLAIMID is null
WHILE EXISTS ( SELECT *
FROM RAWClaimTable
WHERE Processed = 0 )
BEGIN
-----Initialize Default Variables
SET #Lang = 0
SET #Address = 'Import'
SET #SaveMode = 0
SET #ID = Null
SELECT TOP 1
#LossDate = LossDate ,
#ClaimDate = ClaimDate ,
#OpenDate = OpenDate ,
#Receivedate = ReceiVeDate ,
#Name = Name ,
#Address = Address ,
#Address1 = Address1 ,
#City = City ,
#State = State ,
#PostalCode = PostalCode ,
#Country = Country
FROM RAWClaimTable
WHERE Processed = 0
ORDER BY ClaimID
BEGIN TRY
EXEC #RetStat = Usp_ProcessRawData #Lang, #Address, #SaveMode, #ID OUT,
#BranchSeqNo, #OriginalBranchSeqNo, #IncidentID,
#AssignmentTypeCode, #PartnershipID, #AccountID,
END TRY
BEGIN CATCH
SELECT #RetStat = ##Error
if ##Trancount > 0 rollback tran
IF #RetStat != 0
BEGIN
update RAWClaimTable set Errormessage = ERROR_MESSAGE() where ClaiMKey = #Name
END
END CATCH
IF #ID IS NOT NULL
BEGIN
UPDATE RAWClaimTable
SET ClaimID = #ID ,
Processed = 1
WHERE ClaiMKey = #Name
END
ELSE
BEGIN
UPDATE RAWClaimTable
SET Processed = 1
WHERE ClaiMKey = #Name
END
END
Use a CURSOR to loop thru your rows!
SET #Lang = 0
SET #Address = 'Import'
SET #SaveMode = 0
SET #ID = Null
DECLARE my_cursor CURSOR FOR
SELECT LossDate, ClaimDate, OpenDate, ReceiVeDate, Name, Address,
Address1, City, State, PostalCode, Country
FROM RAWClaimTable
WHERE Processed = 0
OPEN my_cursor
FETCH NEXT FROM my_cursor
INTO #LossDate, #ClaimDate, #OpenDate, #Receivedate, #Name, #Address,
#Address1, #City, #State, #PostalCode, #Country
WHILE ##FETCH_STATUS = 0
BEGIN
BEGIN TRY
EXEC #RetStat = Usp_ProcessRawData #Lang, #Address, #SaveMode, #ID OUT,
#BranchSeqNo, #OriginalBranchSeqNo, #IncidentID,
#AssignmentTypeCode, #PartnershipID, #AccountID,
END TRY
BEGIN CATCH
SELECT #RetStat = ##Error
if ##Trancount > 0 rollback tran
IF #RetStat != 0
BEGIN
update RAWClaimTable set Errormessage = ERROR_MESSAGE()
where ClaiMKey = #Name
END
END CATCH
IF #ID IS NOT NULL
BEGIN
UPDATE RAWClaimTable
SET ClaimID = #ID ,
Processed = 1
WHERE ClaiMKey = #Name
END
ELSE
BEGIN
UPDATE RAWClaimTable
SET Processed = 1
WHERE ClaiMKey = #Name
END
FETCH NEXT FROM my_cursor
INTO #LossDate, #ClaimDate, #OpenDate, #Receivedate, #Name, #Address,
#Address1, #City, #State, #PostalCode, #Country
END