SQL Server stored procedure expects parameter which was not supplied - sql-server

CREATE TABLE [dbo].[review]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[uID] [varchar](6) NOT NULL,
[pID] [int] NOT NULL,
[email] [nvarchar](255) NOT NULL,
[review] [nvarchar](3000) NULL,
[refURL] [nvarchar](2083) NOT NULL,
[refID] [nvarchar](100) NOT NULL,
[cDate] [datetime] NOT NULL,
CONSTRAINT [PK_review]
PRIMARY KEY CLUSTERED ([id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[review]
ADD CONSTRAINT [DF_review_uID] DEFAULT (LEFT(NEWID(), (6))) FOR [uID]
GO
ALTER TABLE [dbo].[review]
ADD CONSTRAINT [DF_review_cDate] DEFAULT (GETDATE()) FOR [cDate]
GO
I wrote this stored procedure:
ALTER PROCEDURE [dbo].[spReview]
#id INT = 0,
#uID VARCHAR(6),
#pID INT = 0,
#email NVARCHAR(255),
#review NVARCHAR(3000),
#refURL NVARCHAR(2083),
#refID NVARCHAR(100),
#cDate DATETME = NULL,
#OPERATION NVARCHAR(50) = ''
AS
IF #OPERATION = 'Insert'
BEGIN
DECLARE #inserted TABLE ([uID] VARCHAR(6));
INSERT INTO review ([pID], [email], [review], [refURL], [refID])
OUTPUT INSERTED.[uID] INTO #inserted
VALUES (#pID, #email, #review, #refURL, #refID)
SELECT *
FROM #inserted
END
ELSE IF #OPERATION = 'Delete'
BEGIN
DELETE FROM review
WHERE id = #id
END
ELSE IF #OPERATION = 'Update'
BEGIN
UPDATE review
SET pID = #pID,
email = #email,
review = #review,
refURL = #refURL,
refID = #refID
WHERE id = #id
END
uID : left(newid(),(6)) and cDate : getdate() set default value
DECLARE #return_value int
EXEC #return_value = [dbo].[spReview]
#id = N'29',
#OPERATION = N'Delete'
SELECT 'Return Value' = #return_value
GO
I get this error when I execute the delete query:
Procedure or function 'spReview' expects parameter '#uID', which was not supplied
I tried debugging, I can't figure out where I made a mistake. Where did I make a mistake?
ELSE IF #OPERATION = 'Delete'
BEGIN
DELETE FROM review
WHERE id = #id
END
Just waiting for '#id' parameter, it doesn't need '#uID'

This is your code:
ALTER PROCEDURE [dbo].[spReview]
#id INT = 0,
#uID VARCHAR(6),
#pID INT = 0,
#email NVARCHAR(255),
#review NVARCHAR(3000),
#refURL NVARCHAR(2083),
#refID NVARCHAR(100),
#cDate DATETME = NULL,
#OPERATION NVARCHAR(50) = ''
For every parameter without an "=" sign, you need to provide a value. In your case, it means that you need to provide at least the following parameters:
#uID VARCHAR(6),
#email NVARCHAR(255),
#review NVARCHAR(3000),
#refURL NVARCHAR(2083),
#refID NVARCHAR(100),
So either you provide these parameters when calling the procedure, or you rewrite your procedure such that you don't need these parameters.

As mentioned, multiple times, this should be 3 separate SPs, this way you only need the parameters you need for that operation:
CREATE PROC dbo.Review_Delete #id int AS
BEGIN
DELETE FROM dbo.review
WHERE id = #id;
END;
GO
CREATE PROC dbo.Review_Insert #pID int, #email nvarchar(255), #review nvarchar(3000), #RefURL nvarchar(2083), #RefID nvarchar(100) AS
BEGIN
INSERT INTO dbo.review(pID, email, review, refURL, refID)
OUTPUT inserted.uID --This seems like an OUTPUT parameter might be better, as you insert a single row
VALUES(#pID, #Email, #review, #RefURL, #RefID);
END;
GO
CREATE PROC dbo.Review_Update #id int, #pID int, #email nvarchar(255), #review nvarchar(3000), #RefURL nvarchar(2083), #RefID nvarchar(100) AS
BEGIN
UPDATE dbo.review
SET pID = #pID,
email = #email,
review = #review,
refURL = #refURL,
refID = #refID
WHERE ID = #ID;
END;
GO
Notice I never declare the parameter #cDate, as you don't use it once in your SP.
If, for some really odd reason, you really need to have one SP, then create the others and call them dynamically; only passing the parameters you passed to the "master" SP to the "children". I, however, don't recommend this one and you should just call them correct one in the first place:
CREATE PROC Review_Operation #Operation char(6), --No need for this to be an nvarchar, or 50 characters, delete, insert and update are all 6 characters in length and contain no unicode characters
#ID int = NULL, #pID int = NULL, #email nvarchar(255) = NULL, #review nvarchar(3000) = NULL, #RefURL nvarchar(2083) = NULL, #RefID nvarchar(100) = NULL AS
BEGIN
--Because they are all NULL we're going to use Dynamic SQKL to only pass parameters will a value to force the error
DECLARE #SQL nvarchar(MAX),
#Params nvarchar(MAX);
IF #Operation = 'Delete' BEGIN
SET #SQL = N'EXEC Review_Delete ' + CASE WHEN #ID IS NOT NULL THEN N'#id' ELSE N'' END + N';';
SET #Params = N'#ID int';
EXEC sp_executesql #SQL, #Params, #ID;
END ELSE IF #Operation = 'Insert' BEGIN
SET #SQL = N'EXEC Review_Insert ' + STUFF(CASE WHEN #pID IS NOT NULL THEN N',#pID = #pID' ELSE N'' END +
CASE WHEN #email IS NOT NULL THEN N',#email = #email' ELSE N'' END +
CASE WHEN #review IS NOT NULL THEN N',#review = #review' ELSE N'' END +
CASE WHEN #RefURL IS NOT NULL THEN N',#RefURL = #RefURL' ELSE N'' END +
CASE WHEN #RefID IS NOT NULL THEN N',#RefID = #RefID' ELSE N'' END,1,1,N'') + N';';
SET #Params = N'#pID int, #email nvarchar(255), #review nvarchar(3000), #RefURL nvarchar(2083), #RefID nvarchar(100)';
EXEC sp_executesql #SQL, #Params, #pID, #email, #review, #RefURL, #RefID;
END ELSE IF #Operation = 'Update' BEGIN
SET #SQL = N'EXEC Review_Update ' + STUFF(CASE WHEN #ID IS NOT NULL THEN N',#ID = #ID' ELSE N'' END +
CASE WHEN #pID IS NOT NULL THEN N',#pID = #pID' ELSE N'' END +
CASE WHEN #email IS NOT NULL THEN N',#email = #email' ELSE N'' END +
CASE WHEN #review IS NOT NULL THEN N',#review = #review' ELSE N'' END +
CASE WHEN #RefURL IS NOT NULL THEN N',#RefURL = #RefURL' ELSE N'' END +
CASE WHEN #RefID IS NOT NULL THEN N',#RefID = #RefID' ELSE N'' END,1,1,N'') + N';';
SET #Params = N'#id int,#pID int, #email nvarchar(255), #review nvarchar(3000), #RefURL nvarchar(2083), #RefID nvarchar(100)';
EXEC sp_executesql #SQL, #Params, #ID, #pID, #email, #review, #RefURL, #RefID;
END;
END;
GO

Procedure or function 'spReview' expects parameter '#uID', which was
not supplie
This means you are not passing a value to #uID when you are calling the stored procedure. You need to pass a valid parameter during SP execution.

You have couple of issues with your procedure creation and calling.
Creation: You declared a parameter #uID VARCHAR(6) with no use in the Procedure. You can remove the line #uID VARCHAR(6) from the procedur's parameter definition section.
Calling: you defined parameter - #email, #review, #refURL, #refID in the procedure but not providing values to them while calling the procedure. You should call the procedure as below-
DECLARE #return_value int
EXEC #return_value = [dbo].[spReview]
#id = N'29',
#email = N'abc#yahoo.com',
#OPERATION = N'Delete',
#email = 'test#yahoo.com',
#review = 'abc',
#refURL = 'xyz',
#refID = '1' -- ID should be a INT but declared as NVARCHAR
SELECT 'Return Value' = #return_value
GO

Though not really recommended as stated above, you can provide default values, and in fact you have default values set for #id, #pID, and #OPERATION.
Of course, if you default #uID to null, you MUST account for that in the actual procedure to make sure a null #uID won't cause other errors.
create proc [dbo].[spReview]
#id INT = 0,
#uID VARCHAR(6) = null,
#pID INT = 0,
#email NVARCHAR(255),
#review NVARCHAR(3000),
#refURL NVARCHAR(2083),
#refID NVARCHAR(100),
#cDate DATETME = NULL,
#OPERATION NVARCHAR(50) = ''
AS
if #uID is null
begin
-- do something here when #uID is null.
end

Related

SQL Server trigger double values at first insert

I created a trigger on Ames.Details to fire Ames.Sqlclass but when I insert the data into Ames.Details, for example ‘Ken’, ’Math’, 20, ’In’, it shows me 40 marks in Sqlclass.Syntax** below
CREATE SCHEMA Ames
GO
CREATE TABLE Ames.SqlClass
(
Name NVARCHAR(50),
Subject NVARCHAR(50),
Mark INT
)
GO
CREATE TABLE Ames.Details
(
Name NVARCHAR(50),
Subject NVARCHAR(50),
Mark INT,
Status NVARCHAR(20)
)
GO
CREATE TRIGGER SQlDetails
ON Ames.Details
AFTER INSERT
AS
BEGIN
DECLARE #st INT, #Namesql NVARCHAR(50),
#Subject NVARCHAR(50), #pt INT, #Status NVARCHAR(20)
SELECT
#Namesql = i.Name, #Subject = i.subject,
#pt = i.mark, #Status = I.Status
FROM
inserted AS I
IF NOT EXISTS (SELECT * FROM Ames.SqlClass
WHERE #Namesql = Name AND #Subject = Subject)
BEGIN
INSERT INTO Ames.sqlClass (Name, Subject, Mark)
VALUES (#Namesql, #Subject, #pt)
END
SELECT #st = Mark
FROM ames.SqlClass
WHERE #Namesql = Name AND #Subject = Subject
BEGIN
IF #Status = 'IN'
BEGIN
SET #st = #st + #pt
END
IF #Status = 'Out'
BEGIN
SET #st = #st - #pt
END
UPDATE Ames.SqlClass
SET Mark = #st
WHERE Name = #Namesql
END
END

T-SQL stuck on scope_identity return from stored procedure

I'm sure this is a easy fix, but I can't find an answer here. Been many a years since I have written stored procedures..
These are my procedures:
This first one works, and returns the newly created Id.
ALTER PROCEDURE [dbo].[sp_CreateBytegymType]
#Name NVARCHAR(200),
#Type NCHAR(1),
#Description NVARCHAR(MAX) NULL,
#Comment NVARCHAR(MAX) NULL,
#Source NVARCHAR(MAX) NULL,
#BtId INT OUTPUT
AS
BEGIN
SET NOCOUNT ON
INSERT INTO BytegymType
VALUES (#Name, #Type, #Description, #Comment, #Source)
SET #BtId = CAST(SCOPE_IDENTITY() AS INT)
END
The second one calls the first one:
ALTER PROCEDURE [dbo].[sp_CreateMuscle]
#Name NVARCHAR(200),
#Type NCHAR(1),
#Description NVARCHAR(MAX) NULL,
#Comment NVARCHAR(MAX) NULL,
#Source NVARCHAR(MAX) NULL,
#Group NVARCHAR(20) NULL
AS
BEGIN
DECLARE #BtId int
EXEC sp_CreateBytegymType
#Name = #Name,
#Type = #Type,
#Description = #Description,
#Comment = #Comment,
#Source = #Source,
#BtId = #BtId
INSERT INTO Muscle
VALUES (#BtId, #Group)
END
I get the following error:
Msg 515, Level 16, State 2, Procedure sp_CreateMuscle, Line 20 [Batch Start Line 2]
Cannot insert the value NULL into column 'BtId'
Seems I'm not keeping the #BtId value. Do I need to put it into a new value after executing the sp_CreateBytegymType?
Also I would like to do this in a transactional manner. So if the the insert into Muscle fails, it should rollback the stored procedure insert.
You need to add OUTPUT:
exec sp_CreateBytegymType
#Name = #Name,
#Type = #Type,
#Description = #Description,
#Comment = #Comment,
#Source = #Source,
#BtId = #BtId OUTPUT;
Also prefixing user defined stored procedures with sp_ is not advised. Related article: Is the sp_ prefix still a no-no?

How to get the last identity value using sp_executesql in StoredProcedure?

I am inserting records using stored procedure and sp_executesql. Once I insert record using sp_executesql, i need the last inserted identity field value on that session.
ALTER proc [dbo].[spHoldTransaction]
#RegisterNo int,
#StoreID int,
#Department varchar(50),
#TransactionDateFrom date,
#TransactionDateTo date,
#Comment Varchar(50)
AS
BEGIN
DECLARE #RegisterID int;
DECLARE #DatabaseName varchar(15);
DECLARE #Batch int;
SELECT #RegisterID=ID FROM Register WHERE Register.Number = #RegisterNo;
SELECT #Batch = BatchNumber From Batch WHERE Status = 0 and RegisterID = #RegisterID
SET #DatabaseName = 'xxx'
SELECT #Department=''''+REPLACE(#Department,',',''',''')+''''
DECLARE #Qry nvarchar(MAX);
DECLARE #ParamDefinition nvarchar(MAX);
SET #ParamDefinition = N'#comment nvarchar(50),#StoreID int,#Batch int'
SET #Qry = '
INSERT INTO '+#DatabaseName+'.dbo.TransactionHold
(
[StoreID]
,[HoldComment]
,[BatchNumber]
,[ShippingNotes]
)
SELECT
#StoreID AS [StoreID]
,#Comment AS [HoldComment]
,#Batch AS [BatchNumber]
,'''' AS [ShippingNotes];
'
EXECUTE sp_executesql #Qry, #ParamDefinition, #Comment, #StoreID, #Batch
SELECT SCOPE_IDENTITY()
END
When I execute this above stored procedure, it's return empty. But TransactionHold has identity column Id
Try retrieving the identity inside the same scope of the execute sql procedure and return the value as an OUT parameter. Do these changes:
SET #ParamDefinition = N'#comment nvarchar(50),#StoreID int,#Batch int, #identity int out'
SET #Qry = '
INSERT INTO '+#DatabaseName+'.dbo.TransactionHold
(
[StoreID]
,[HoldComment]
,[BatchNumber]
,[ShippingNotes]
)
SELECT
#StoreID AS [StoreID]
,#Comment AS [HoldComment]
,#Batch AS [BatchNumber]
,'''' AS [ShippingNotes];
SET #identity = ##IDENTITY
'
DECLARE #identity INT
EXECUTE sp_executesql #Qry, #ParamDefinition, #Comment, #StoreID, #Batch, #identity OUT
SELECT #identity

When I execute the stored procedure I get an error

When I execute the stored procedure, I get this error:
Msg 8114, Level 16, State 5, Procedure SPXML, Line 158
Error converting data type varchar to numeric.
I'm using a stored procedure and cursor for the first time.
This is my stored procedure:
CREATE PROCEDURE [dbo].[SPXML]
(#CounterStockMaster text,
#CounterStockDetails text,
#CounterStock text)
AS
DECLARE #M0 VARCHAR(100) --EditStatus
DECLARE #M1 VARCHAR(100) --Counter_Code
DECLARE #M2 VARCHAR(100) --Counter_Name
DECLARE #M3 VARCHAR(100) --To Branch_Code
DECLARE #D1 VARCHAR(100) --Project Type
DECLARE #D2 VARCHAR(100) --drpC.Text
DECLARE #D3 VARCHAR(100) --grdGO.Rows[i].Cells["1"].Value
DECLARE #D4 VARCHAR(100) --grdGO.Rows[i].Cells["2"].Value
DECLARE #D5 VARCHAR(100)
DECLARE #C1 VARCHAR(100) --Cnt Code
DECLARE #C2 VARCHAR(100) --Item
DECLARE #C3 VARCHAR(100) --Qty
BEGIN
DECLARE #CNTNo VARCHAR(100)
DECLARE #idoc INT
DECLARE #INDate Datetime
DECLARE #Branch_Code NUMERIC(18,0)
DECLARE #ItemCode NUMERIC(18,0)
DECLARE #ItemQty NUMERIC(18,3)
DECLARE #PurRate NUMERIC(18,2)
DECLARE #SaleRate NUMERIC(18,2)
DECLARE #MRP NUMERIC(18,2)
DECLARE #PurDate DATETIME
DECLARE #Batch_No VARCHAR(50)
DECLARE #ExpiryDate DATETIME
DECLARE #MultiMRP BIT
BEGIN TRANSACTION
SET DATEFORMAT dmy
SET #MultiMRP = (Select ISNULL(Multiple_Mrp,0) from [Company])
EXEC sp_xml_preparedocument #idoc OUTPUT, #CounterStockMaster
DECLARE GINMasterCursor CURSOR FOR
SELECT * FROM OPENXML (#idoc, '/CSMASTER/ID',1)
WITH (M0 VARCHAR(100), M1 VARCHAR(100), M2 VARCHAR(100),M3 VARCHAR(100))
OPEN GINMasterCursor
FETCH NEXT FROM GINMasterCursor INTO #M0,#M1,#M2,#M3
IF #M0='T' ---Edit Mode TRUE
BEGIN --- Reversing the Item Stock for the Editing Sales START
SET #CNTNo = #M1
DECLARE GInDetailCursor CURSOR FOR
SELECT Counter_Stock_Code,Item_Code,Item_Qty,Branch_Code From [CntDetails]
WHERE Counter_Stock_Code = #CNTNo AND Branch_Code=#M3
OPEN GInDetailCursor
FETCH NEXT FROM GInDetailCursor INTO #CNTNo,#ItemCode,#ItemQty,#Branch_Code
WHILE ##FETCH_STATUS=0
BEGIN
IF #MultiMRP = 0
UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL - #ItemQty , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = #ItemCode and Type_Code = 0 and Branch_Code = #M3
ELSE
UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL - #ItemQty , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = #ItemCode and Item_MRP = #MRP and Type_Code = 0 and Branch_Code = #M3
FETCH NEXT FROM GInDetailCursor INTO #CNTNo,#ItemCode,#ItemQty,#Branch_Code
END
CLOSE GInDetailCursor
DEALLOCATE GInDetailCursor
END --- Reversing the Item Stock for the Editing GO END
ELSE
BEGIN
SET #CNTNo = (SELECT ISNULL(MAX(Counter_Stock_Code)+1,1) FROM [Counter Stock Master] where Branch_Code = #M3)
END
INSERT INTO [CntMaster]
(Counter_Stock_Code,Counter_Stock_Date,Branch_Code)
VALUES
(#CNTNo, #INDate, #M3)
CLOSE GINMasterCursor
DEALLOCATE GINMasterCursor
EXEC sp_xml_removedocument #idoc
-- Create an internal representation of the XML document.
EXEC sp_xml_preparedocument #idoc OUTPUT, #CounterStockDetails
-- Execute a SELECT statement using OPENXML rowset provider.
DECLARE GInDetailsCursor CURSOR FOR
SELECT * FROM OPENXML (#idoc, '/CSDETAILS/ID',1)
WITH ( D1 VARCHAR(100), D2 VARCHAR(100), D3 VARCHAR(100), D4 VARCHAR(100))
OPEN GInDetailsCursor
FETCH NEXT FROM GInDetailsCursor INTO #D1,#D2,#D3,#D4
WHILE ##FETCH_STATUS = 0
BEGIN
IF #D1='A' or #D1='D' --For ProjectType ==> Departmental Stores
BEGIN
SET #D2 = #CNTNo
INSERT INTO [CntDetails]
(Counter_Stock_Code,Item_Code,Item_Qty,Branch_Code)
VALUES
(#D2, #D3, #D4, #M3)
IF #MultiMRP = 0
UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL + #D4 , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = #D3 and Type_Code = 0 and Branch_Code = #M3
ELSE
UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL + #D4 , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = #D3 and Type_Code = 0 and Branch_Code = #M3
END
FETCH NEXT FROM GInDetailsCursor INTO #D1,#D2,#D3,#D4
END
CLOSE GInDetailsCursor
DEALLOCATE GInDetailsCursor
***[EXEC sp_xml_removedocument #idoc ----------------------- "I get this error from this line"][1]***
-- Create an internal representation of the XML document.
EXEC sp_xml_preparedocument #idoc OUTPUT, #CounterStock
-- Execute a SELECT statement using OPENXML rowset provider.
DECLARE CSCursor CURSOR FOR
SELECT * FROM OPENXML (#idoc, '/CounterStock/ID',1)
WITH ( D1 VARCHAR(100), D2 VARCHAR(100), D3 VARCHAR(100), D4 VARCHAR(100))
OPEN CSCursor
FETCH NEXT FROM CSCursor INTO #D1,#D2,#D3,#D4
WHILE ##FETCH_STATUS = 0
BEGIN
IF #D1='A' or #D1='D' --For ProjectType ==> Departmental Stores
BEGIN
INSERT INTO [CntStock]
(Conter_Code,Item_Code,Item_Qty)
VALUES
(#D2, #D3, #D4)
IF #MultiMRP = 0
UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL + #D4 , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = #D3 and Type_Code = 0 and Branch_Code = #M3
ELSE
UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL + #D4 , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = #D3 and Type_Code = 0 and Branch_Code = #M3
END
FETCH NEXT FROM CSCursor INTO #D1,#D2,#D3,#D4
END
CLOSE CSCursor
DEALLOCATE CSCursor
EXEC sp_xml_removedocument #idoc
SELECT #CNTNo
COMMIT TRANSACTION
END
GO
I tried many times but i didn't solve this problem.
Does anyone have any suggestions?
Error here:
INSERT INTO [CntStock]
(Conter_Code,Item_Code,Item_Qty)
VALUES
(#D2, #D3, #D4) ---DECLARE #D4 VARCHAR(100) ---What is the type of 'Item_Qty' column in CntStock table,Is it numeric,You are assigning varchar to numberic field.
Try changing the type of #D4

How to pass output parameter to a Stored Procedure?

I have written a stored procedure with the following format:
ALTER PROCEDURE usp_data_migration
(#sourceDatabase varchar(50),
#sourceTable varchar(50),
#targetDatabase varchar(50),
#targetTable varchar(50),
#finaloutput varchar(max) output)
AS
BEGIN
----Set of SQL Blocks
END
Then, I am executing the procedure:
DECLARE #finaloutput1 varchar(300)
EXEC usp_data_migration 'Yousuf', 'emp', '[City Branch]', 'emp_tgt', #finaloutput1 output
SELECT #finaloutput1
By executing this way I don't proper output.
When I execute this way:
DECLARE #finaloutput1 varchar(300)
EXEC usp_data_migration #sourceDatabase = 'Yousuf',
#sourceTable = 'emp',
#targetDatabase = '[City Branch]',
#targetTable = 'emp_tgt',
#finaloutput1 output
SELECT #finaloutput1
I get an error message saying:
Msg 119, Level 15, State 1, Line 41
Must pass parameter number 5 and subsequent parameters as '#name = value'. After the form '#name = value' has been used, all subsequent parameters must be passed in the form '#name = value'.
And if I removed my output parameter and execute the procedure, I get my desired output but I am not able to get my result as an output.
EXEC usp_data_migration #sourceDatabase = 'Yousuf',
#sourceTable = 'emp',
#targetDatabase = '[City Branch]',
#targetTable = 'emp_tgt'
What should I do?
Thanks in advance.
The error message is self-explanatory - you should name all of your parameters.
DECLARE #finaloutput1 varchar(300);
EXEC dbo.usp_data_migration -- always use schema prefix
#sourceDatabase = 'Yousuf',
#sourceTable = 'emp',
#targetDatabase = '[City Branch]',
#targetTable = 'emp_tgt',
#finaloutput = #finaloutput1 OUTPUT;
SELECT #finaloutput1;
You have to Select like this
Example 1
create procedure p1
(
#id INT,
#name varchar(20) OUTPUT,
#company varchar(20) OUTPUT
)
AS
BEGIN
Set #name = 'name'
Set #company = 'company'
select #name , #company from table1 where id = #id;
END
GO
Example 2
CREATE PROCEDURE Myproc
#parm varchar(10),
#parm1OUT varchar(30) OUTPUT,
#parm2OUT varchar(30) OUTPUT
AS
SELECT #parm1OUT='parm 1' + #parm
SELECT #parm2OUT='parm 2' + #parm
GO
DECLARE #SQLString NVARCHAR(500)
DECLARE #ParmDefinition NVARCHAR(500)
DECLARE #parmIN VARCHAR(10)
DECLARE #parmRET1 VARCHAR(30)
DECLARE #parmRET2 VARCHAR(30)
SET #parmIN=' returned'
SET #SQLString=N'EXEC Myproc #parm,
#parm1OUT OUTPUT, #parm2OUT OUTPUT'
SET #ParmDefinition=N'#parm varchar(10),
#parm1OUT varchar(30) OUTPUT,
#parm2OUT varchar(30) OUTPUT'
EXECUTE sp_executesql
#SQLString,
#ParmDefinition,
#parm=#parmIN,
#parm1OUT=#parmRET1 OUTPUT,#parm2OUT=#parmRET2 OUTPUT
SELECT #parmRET1 AS "parameter 1", #parmRET2 AS "parameter 2"
go
drop procedure Myproc
Please refer more here
Simple Example:
create procedure proc2 #var int out,#var2 varchar(10) out
as
begin
set #var=(select max(id) from customer);
set #var2=(select name from customer where id=#var);
end
declare #maxid int;
declare #maxname varchar(10);
exec proc2 #maxid out,#maxname out;
select #maxid,#maxname;

Resources