Error Must declare the scalar variable with Table Valued Parameter - c#

I am not getting what I am doing wrong here. From the update segment, I am getting this error:
Must declare the scalar variable "#OrderTestTVP".
Here is my table-valued parameter type
CREATE TYPE [dbo].[TVP_OrderTest] AS TABLE(
[OrderTestId] [int] NULL,
[OrderId] [int] NOT NULL,
[TestCode] [varchar](10) NOT NULL
)
GO
ALTER PROCEDURE [dbo].[TVP_Save_OrderTest]
#OrderTestTVP TVP_OrderTest READONLY
AS
BEGIN
BEGIN TRY
DECLARE #TableOfIdentities TABLE (IdentValue BIGINT, TestCode varchar(10) )
INSERT INTO OrderTest
(
OrderId
,TestCode
)
OUTPUT Inserted.OrderTestId,inserted.TestCode
INTO #TableOfIdentities(IdentValue,TestCode)
SELECT OrderId
,TestCode
FROM #OrderTestTVP
WHERE OrdertestID = 0
UPDATE
OrderTest
SET
OrderId = #OrderTestTVP.OrderId,
TestCode = #OrderTestTVP.TestCode
FROM OrderTest INNER JOIN #OrderTestTVP
ON OrderTest.OrderTestId = #OrderTestTVP.OrderTestId
SELECT * FROM #TableOfIdentities
END TRY
BEGIN CATCH
exec error_catch
END CATCH
END

I'd say you have to use a table alias for the join operation:
UPDATE
OrderTest
SET
OrderId = o.OrderId,
TestCode = o.TestCode
FROM OrderTest INNER JOIN #OrderTestTVP o
ON OrderTest.OrderTestId = o.OrderTestId
I have no SQL at hand right now to try but if I recall correctly, table variables need aliases if used in expressions.

Related

C# stored procedure insert or update with DataTable problem?

I want to add or update with the code below. I get the following error. How should I edit this code?
Must declare the scalar variable "#UyeID".
Incorrect syntax near the keyword 'ELSE'.
My code:
CREATE TYPE [dbo].[BcgTarihiType] AS TABLE
(
[UyeID] int NOT NULL,
[BcgTarihi] [nvarchar](30) NULL
)
GO
CREATE PROCEDURE Insert_BcgTarihleri
(#tblBcgTarihleri BcgTarihiType READONLY)
AS
BEGIN
IF EXISTS (SELECT 1 FROM BcgTarihleri
WHERE UyeID = #UyeID AND BcgTarihi = #BcgTarihi)
BEGIN
UPDATE BcgTarihleri
SET UyeID = #UyeID, BcgTarihi = #BcgTarihi
WHERE UyeID = #UyeID
END
ELSE
BEGIN
INSERT INTO BcgTarihleri
VALUES (#UyeID, #BcgTarihi)
END
END
I also want to ask the question in this way. The code below works but I can only add.How can this update if exist data?
CREATE TYPE [dbo].[BcgTarihiType] AS TABLE(
[UyeID] int NOT NULL,
[BcgTarihi] [nvarchar](30) NULL
)
GO
CREATE PROCEDURE Insert_BcgTarihleri
(
#tblBcgTarihleri BcgTarihiType READONLY
)
AS
BEGIN
INSERT into BcgTarihleri (UyeID,BcgTarihi)
SELECT UyeID, BcgTarihi FROM #tblBcgTarihleri;
END
#Boris Makhlin, #Dale K Thank you for your help I solved my problem with the code below
CREATE TYPE [dbo].[BcgTarihiType] AS TABLE(
[UyeID] int NOT NULL,
[BcgTarihi] [nvarchar](30) NULL
)
GO
CREATE PROCEDURE [dbo].[Update_BcgTarihleri]
#tblBcgTarihleri BcgTarihiType READONLY
AS
BEGIN
SET NOCOUNT ON;
MERGE INTO BcgTarihleri b1
USING #tblBcgTarihleri b2
ON b1.UyeID = b2.UyeID AND b1.BcgTarihi = b2.BcgTarihi
WHEN MATCHED THEN
UPDATE SET b1.UyeID = b2.UyeID
,b1.BcgTarihi = b2.BcgTarihi
WHEN NOT MATCHED THEN
INSERT VALUES(b2.UyeID, b2.BcgTarihi);
END

Procedure not working because of unresolved reference to object

I'm writing a WPF application where at some point I'm trying to add new row to my database through procedure like below:
CREATE PROCEDURE dbo.InsertStudent
#IdStudent INT,
#FirstName VARCHAR(50),
#LastName VARCHAR(50),
#Address VARCHAR(50),
#IndexNumber VARCHAR(50),
#IdStudies INT
AS
SET NOCOUNT ON
INSERT INTO [dbo].[apbd.Student]
([IdStudent]
,[FirstName]
,[LastName]
,[Address]
,[IndexNumber]
,[IdStudies])
VALUES
(#IdStudent
,#FirstName
,#LastName
,#Address
,#IndexNumber
,#IdStudies)
but whenever I'm about to use it, I'm getting error:
SQL71502: Procedure: [dbo].[InsertStudent] has an unresolved reference to object [dbo].[apbd.Student].
I was looking for solution but what I've found was only to add reference to database through right click on References and so on, but I do not have this option in my solution explorer.
Maybe I'm looking for it in wrong places but the only options I have after right click are something like this:
Add reference...
Add reference to service...
Add connected/concatenated/accumulative (or however should it be translated) service
Add analyzer...
Manage NuGet packets...
as for the code behind creation of the tables in database:
CREATE SCHEMA apbd;
GO
-- tables
-- Table: Student
CREATE TABLE apbd.Student (
IdStudent int NOT NULL IDENTITY,
FirstName nvarchar(100) NOT NULL,
LastName nvarchar(100) NOT NULL,
Address nvarchar(100) NOT NULL,
IndexNumber nvarchar(50) NOT NULL,
IdStudies int NOT NULL,
CONSTRAINT Student_pk PRIMARY KEY (IdStudent)
);
-- Table: Student_Subject
CREATE TABLE apbd.Student_Subject (
IdStudentSubject int NOT NULL IDENTITY,
IdStudent int NOT NULL,
IdSubject int NOT NULL,
CreatedAt datetime NOT NULL,
CONSTRAINT Student_Subject_pk PRIMARY KEY (IdStudentSubject,IdStudent,IdSubject)
);
-- Table: Studies
CREATE TABLE apbd.Studies (
IdStudies int NOT NULL IDENTITY,
Name nvarchar(100) NOT NULL,
CONSTRAINT Studies_pk PRIMARY KEY (IdStudies)
);
-- Table: Subject
CREATE TABLE apbd.Subject (
IdSubject int NOT NULL IDENTITY,
Name nvarchar(100) NOT NULL,
CONSTRAINT Subject_pk PRIMARY KEY (IdSubject)
);
-- End of file.
A MS SQL Server database, by default, only has a single schema (dbo). You can add schemas to group things for either security or organizational purposes.
In your case, the schema apbd was created and Student was created on that schema not the dbo schema. So, to reference that table, you need to use [apbd].[Student].
I would run the following to determine the actual name and schema of the table:
SELECT
CAST(
MAX(
CASE
WHEN
TABLE_SCHEMA = 'apbd'
AND TABLE_NAME = 'Student'
THEN 1
ELSE 0
END
) AS bit
) [The table is apbd.Student]
,
CAST(
MAX(
CASE
WHEN
TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'apbd.Student'
THEN 1
ELSE 0
END
) AS bit
) [The table is dbo.apbd.Student]
FROM INFORMATION_SCHEMA.TABLES
I'm also wondering if you perhaps need a USE statement at the start of your CREATE script - are you creating the procedure on the right database?
If the table is on a different database you would need to reference the database in your stored procedure, i.e. [DatabaseName].[dbo].[apbd.Student].

How to insert the foreign key into a subtype table in SQL Server

I have a supertype table called Student, and its subtype table called OtherStudents. I am wondering how to write a stored procedure that will insert the foreign key value into OtherStudents table every time a new record with a student type Other is inserted into Student.
Student table:
CREATE TABLE [dbo].[Student]
(
[STUD_ID] [INT] IDENTITY(1000009,1) NOT NULL,
[STUD_NAM] [VARCHAR](30) NOT NULL,
[STUD_EMAIL] [VARCHAR](50) NULL,
[CAMP_NAM] [VARCHAR](50) NULL,
[CAMP_ZIP] [INT] NULL,
[STUD_TYP] [CHAR](5) NOT NULL,
CONSTRAINT [PK_Student]
PRIMARY KEY CLUSTERED ([STUD_ID] ASC)
)
GO
ALTER TABLE [dbo].[Student] WITH CHECK
ADD CONSTRAINT [FK_Student_Campus]
FOREIGN KEY([CAMP_NAM], [CAMP_ZIP]) REFERENCES [dbo].[Campus] ([CAMP_NAM], [CAMP_ZIP])
GO
ALTER TABLE [dbo].[Student] CHECK CONSTRAINT [FK_Student_Campus]
GO
`OtherStudents` table:
CREATE TABLE [dbo].[OtherStudents]
(
[OSTUD_ID] [INT] IDENTITY(1000009,1) NOT NULL,
[STUD_ST] [VARCHAR](30) NULL,
[STUD_APT] [VARCHAR](5) NULL,
[STUD_CITY] [CHAR](6) NULL,
[STUD_STATE] [CHAR](2) NULL,
[STUD_ZIP] [INT] NULL,
[RPT_ATTM] [VARBINARY](4000) NULL,
[RPT_EYESCR] [VARBINARY](4000) NULL,
[DATE_LASTPASS] [DATE] NULL,
[DATE_LASTVSP] [DATE] NULL,
[STUD_ID] [INT] NULL,
CONSTRAINT [PK_OtherStudents]
PRIMARY KEY CLUSTERED ([OSTUD_ID] ASC)
)
GO
ALTER TABLE [dbo].[OtherStudents] WITH CHECK
ADD CONSTRAINT [FK_OtherStudents_Student]
FOREIGN KEY([STUD_ID]) REFERENCES [dbo].[Student] ([STUD_ID])
GO
ALTER TABLE [dbo].[OtherStudents] CHECK CONSTRAINT [FK_OtherStudents_Student]
GO
I wrote two stored procedures:
ALTER PROCEDURE [dbo].[BusPassStudents_Insert]
(#STUD_ST VARCHAR(30),
#STUD_APT VARCHAR(5),
#STUD_CITY CHAR(6),
#STUD_STATE CHAR(2),
#STUD_ZIP INT,
#RPT_ATTM AS VARBINARY(4000) = NULL,
#RPT_EYESCR AS VARBINARY(4000) = NULL,
#DATE_LASTPASS DATE,
#DATE_LASTVSP AS DATE = NULL)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.OtherStudents (STUD_ST, STUD_CITY, STUD_STATE, STUD_ZIP, RPT_ATTM, RPT_EYESCR,DATE_LASTPASS, DATE_LASTVSP)
VALUES (#STUD_ST, #STUD_CITY, #STUD_STATE, #STUD_ZIP, #RPT_ATTM, #RPT_EYESCR, #DATE_LASTPASS, #DATE_LASTVSP)
END
ALTER PROCEDURE [dbo].[Stud_InsertNew]
(#STUD_NAM VARCHAR(30),
#STUD_EMAIL VARCHAR(50),
#CAMP_NAM VARCHAR(50),
#CAMP_ZIP INT,
#STUD_TYP CHAR(5))
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.Student(STUD_NAM, STUD_EMAIL, CAMP_NAM, CAMP_ZIP, STUD_TYP)
VALUES (#STUD_NAM, #STUD_EMAIL, #CAMP_NAM, #CAMP_ZIP, #STUD_TYP);
DECLARE #STUD_ID INT
SET #STUD_ID = SCOPE_IDENTITY()
SELECT #STUD_ID
WHERE #STUD_TYP = 'Other'
END
Here is my C# code I have:
int campzip = int.Parse(ddlCamp.SelectedValue.Trim());
int StudentZip = int.Parse(txtStdZip.Text);
DateTime DateLastPass = DateTime.ParseExact(txtDateLastPass.Text, "yyyy-MM-dd", null);
using (var connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("cis-laredoConnectionString")))
{
connection.Open();
var cmd = new SqlCommand("dbo.Stud_InsertNew", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#STUD_NAM", txtStdName.Text);
cmd.Parameters.AddWithValue("#STUD_EMAIL", txtStdEmail.Text);
cmd.Parameters.AddWithValue("#CAMP_NAM", ddlCamp.SelectedItem.ToString());
cmd.Parameters.AddWithValue("#CAMP_ZIP", campzip);
cmd.Parameters.AddWithValue("#STUD_TYP", "Other");
int getStudID = (int) cmd.ExecuteScalar();
cmd.Dispose();
connection.Close();
connection.Open();
var cmd2 = new SqlCommand("BusPassStudents_Insert", connection);
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.Parameters.AddWithValue("#STUD_ID", getStudID);
cmd2.Parameters.AddWithValue("#STUD_ST", txtStdStreet.Text);
cmd2.Parameters.AddWithValue("#STUD_APT", txtStdApt.Text);
cmd2.Parameters.AddWithValue("#STUD_CITY", txtStdCity.Text);
cmd2.Parameters.AddWithValue("#STUD_STATE", txtStdState.Text);
cmd2.Parameters.AddWithValue("#STUD_ZIP", StudentZip);
cmd2.Parameters.AddWithValue("#RPT_ATTM", fuAttend.FileBytes);
cmd2.Parameters.Add("#DATE_LASTPASS", SqlDbType.Date).Value = DateLastPass;
cmd2.ExecuteNonQuery();
cmd2.Dispose();
connection.Close();
}
I am trying to use ExecuteScalar to retrieve the inserted STUD_ID in Student table and post it back into the OtherStudents table.
I am getting error:
System.InvalidCastException: 'Specified cast is not valid.'
Any help would be appreciated!
You have to select something from your stored procedure to get the scalar value in the C# which is you missed, so in your Stud_InsertNew you can select the STUD_ID and use ExecuteScalar() in C# code as:
int getStudID = (int)cmd.ExecuteScalar();
Changes in stored procedure:
ALTER PROCEDURE [dbo].[Stud_InsertNew]
(#STUD_NAM VARCHAR(30),
#STUD_EMAIL VARCHAR(50),
#CAMP_NAM VARCHAR(50),
#CAMP_ZIP INT,
#STUD_TYP CHAR(5))
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.Student(STUD_NAM, STUD_EMAIL, CAMP_NAM, CAMP_ZIP, STUD_TYP)
VALUES (#STUD_NAM, #STUD_EMAIL, #CAMP_NAM, #CAMP_ZIP, #STUD_TYP);
// Need this line which returns the latest inserted Student Id
SELECT TOP 1 [STUD_ID]
FROM dbo.Student
ORDER BY [STUD_ID] DESC
END
After a comment from Mitch Wheat and also found here, that the best way to use SCOPE_IDENTIY() if you want to get the latest inserted records primary key's value, so you just need to change the SELECT statement to this:
DECLARE #STUD_ID INT
SET #STUD_ID = SCOPE_IDENTITY()
SELECT #STUD_ID
This is how I'd like to approach this.
Convert two procedures into a single one by passing all the required parameters.
Make use of the "inserted" table data to get the information that was just Inserted into table A and store the reference in table B.
*(i) You may want to pass the rest of the parameters required to store the data in table B. I haven't included them in the stored procedure.
(ii) I'm trying to insert data into table B if only the check on birthday passes. In your case the predicate/condition will differ.
USE SOMEDB;
CREATE TABLE A
(Id Integer IDENTITY(1,1),
[Name] VARCHAR(20),
[Birthday] DATE
)
create table B
(
B_Id INTEGER IDENTITY(10001,1),
A_Id Integer NULL,
ColumnX VARCHAR(20),
ColumnY varchar(20))
CREATE PROCEDURE [uspInsertIndividualDetails]
(#name varchar(20),
#birthday date
)
AS
BEGIN
CREATE TABLE #tmpXYZ(iD INT,birthday date);
INSERT INTO A([Name],[Birthday])
OUTPUT inserted.Id,inserted.Birthday into #tmpXYZ
select #name, #birthday
IF exists (select 1 from #tmpXYZ where Birthday >'2018-01-01')
begin
INSERT INTO B(A_Id,ColumnX, ColumnY)
SELECT Id,'A Value', 'Some Other Value' FROM #tmpXYZ
end
END
execute [uspInsertIndividualDetails] #name ='John Doe', #birthday = '1972-10-01'
SELECT * FROM a
SELECT * FROM b
execute [uspInsertIndividualDetails] #name ='John DoeJr', #birthday = '2018-10-01'
SELECT * FROM a
SELECT * FROM b
Hope this helps.
You can do this without making database connection twice by making one stored procedure which combines the two stored procedure's input parameters as:
Reformed stored procedure:
ALTER PROCEDURE [dbo].[Stud_InsertNew]
(#STUD_NAM VARCHAR(30),
#STUD_EMAIL VARCHAR(50),
#CAMP_NAM VARCHAR(50),
#CAMP_ZIP INT,
#STUD_TYP CHAR(5),
// Parameters for other student
#STUD_APT VARCHAR(5),
#STUD_CITY CHAR(6),
#STUD_STATE CHAR(2),
#STUD_ZIP INT,
#RPT_ATTM AS VARBINARY(4000) = NULL,
#RPT_EYESCR AS VARBINARY(4000) = NULL,
#DATE_LASTPASS DATE,
#DATE_LASTVSP AS DATE = NULL)
AS
BEGIN
DECLARE #OTHERCONSTANT NVARCHAR(MAX)
SET #OTHERCONSTANT = 'Other' // You can set to a text that coming from C#
SET NOCOUNT ON
INSERT INTO dbo.Student(STUD_NAM, STUD_EMAIL, CAMP_NAM, CAMP_ZIP, STUD_TYP)
VALUES (#STUD_NAM, #STUD_EMAIL, #CAMP_NAM, #CAMP_ZIP, #STUD_TYP);
IF(#STUD_TYP = #OTHERCONSTANT)
BEGIN
// Need this line which returns the latest inserted Student Id
DECLARE #STUD_ID INT = NULL
SET #STUD_ID = SCOPE_IDENTITY()
INSERT INTO dbo.OtherStudents (STUD_ST, STUD_CITY, STUD_STATE, STUD_ZIP, RPT_ATTM, RPT_EYESCR,DATE_LASTPASS, DATE_LASTVSP)
VALUES (#STUD_ST, #STUD_CITY, #STUD_STATE, #STUD_ZIP, #RPT_ATTM, #RPT_EYESCR, #DATE_LASTPASS, #DATE_LASTVSP)
END
END
C# code:
using(var connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("cis-laredoConnectionString"))) {
connection.Open();
var cmd = new SqlCommand("dbo.Stud_InsertNew", connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#STUD_NAM", txtStdName.Text);
cmd.Parameters.AddWithValue("#STUD_EMAIL", txtStdEmail.Text);
cmd.Parameters.AddWithValue("#CAMP_NAM", ddlCamp.SelectedItem.ToString());
cmd.Parameters.AddWithValue("#CAMP_ZIP", campzip);
cmd.Parameters.AddWithValue("#STUD_TYP", "Other");
cmd.Parameters.AddWithValue("#STUD_ST", txtStdStreet.Text);
cmd.Parameters.AddWithValue("#STUD_APT", txtStdApt.Text);
cmd.Parameters.AddWithValue("#STUD_CITY", txtStdCity.Text);
cmd.Parameters.AddWithValue("#STUD_STATE", txtStdState.Text);
cmd.Parameters.AddWithValue("#STUD_ZIP", StudentZip);
cmd.Parameters.AddWithValue("#RPT_ATTM", fuAttend.FileBytes);
cmd.Parameters.Add("#DATE_LASTPASS", SqlDbType.Date).Value = DateLastPass;
cmd.ExecuteNonQuery();
cmd.Dispose();
if (connection.State == ConnectionState.Open) {
connection.Close();
}
}

Executing SQL Function Using Linq

I have created a function in MSSQL 2005 and I want to run this function using linq. My function has 2 parameters. The definition is seen below:
USE [MobileGateway]
GO
/****** Object: UserDefinedFunction [dbo].[Fn_GetNoLocationAddress] Script Date: 11/07/2012 08:27:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Fn_GetNoLocationAddress]
(
-- Add the parameters for the function here
#Site nvarchar(255),
#ReceivedDate int
)
RETURNS #noLocationAddress TABLE (
RequestID int NOT NULL,
Barcode varchar(50) NOT NULL,
AdrID int NOT NULL,
Name varchar(50) NOT NULL,
Street varchar(50) NOT NULL,
HouseNo varchar(4) NOT NULL,
Postal varchar(8) NOT NULL,
City varchar(50) NOT NULL,
Country varchar(50) NOT NULL,
Latitude varchar(50) NOT NULL,
Longitude varchar(50) NOT NULL,
ReveivedDate datetime NOT NULL
)
AS
BEGIN
-- Add the SELECT statement with parameter references here
INSERT INTO #noLocationAddress
SELECT TOP (100) PERCENT Request1.RequestID, TrackIT.dbo.Sending.Barcode, TrackIT.dbo.Address_View.AdrID, TrackIT.dbo.Address_View.Name,
TrackIT.dbo.Address_View.Street, TrackIT.dbo.Address_View.HouseNo, TrackIT.dbo.Address_View.Postal, TrackIT.dbo.Address_View.City,
TrackIT.dbo.Address_View.Country, Request1.Latitude, Request1.Longitude, Request1.ReceivedDate
FROM (SELECT DISTINCT RequestID, LTRIM([Content]) AS Barcode, Latitude, Longitude, ReceivedDate
FROM dbo.RequestWithLocation
WHERE (Site LIKE #Site) AND ([Content] <> '') AND (AddressID = '0') AND (ReceivedDate > DATEADD(day, -#ReceivedDate, GETDATE()))) AS Request1 INNER JOIN
TrackIT.dbo.Sending ON Request1.Barcode = TrackIT.dbo.Sending.Barcode INNER JOIN
TrackIT.dbo.Address_View ON TrackIT.dbo.Sending.DeliveryAdrID = TrackIT.dbo.Address_View.AdrID
ORDER BY TrackIT.dbo.Address_View.AdrID
RETURN
END
As you can see I have 2 parameters and I'm returning a table with the information. But I need to use linq for executing the this function.
Can anyone help? thanks
I am assuming that you are using a dbml (linq to sql) file.
Drag drop your User defined function from the server explorer in visual studio into your dbml.
Then from the datacontext object of your dbml you can call the function directly.
For example if your dbml file name is xyz.dbml then your datacontext object's name would be 'XyzDataContext', unless you have changed it.
Then try this.
XyzDataContext db = new XyzDataContext();
List<Fn_GetNoLocationAddressResult> = db.Fn_GetNoLocationAddress("site", 25).ToList();

insert into a table and return a value with procedure

I want to create a procedure that inserts into a table and returns it's primary key.
The primary key of table is uniqueidentyfire and it's guid is true
This is the stored procedure:
ALTER PROCEDURE [dbo].[pr_Tbl_Erae_Insert]
#sfk_Dars varchar(20),
#sfk_Ostad varchar(20),
#byfk_Gerayesh tinyint,
#ifk_term int,
#guidErae_ID uniqueidentifier,
#byGroup_Number tinyint,
AS
-- INSERT a new row in the table.
INSERT [dbo].[Tbl_Erae]
(
[fk_Dars],
[fk_Ostad],
[fk_Gerayesh],
[fk_term],
[Erae_ID],
[Group_Number]
)
VALUES
(
#sfk_Dars,
#sfk_Ostad,
#byfk_Gerayesh,
#ifk_term,
ISNULL(#guidErae_ID, (newid())),
#byGroup_Number
)
I need to return Erae_Id when it inserted into table how can I do that?
Can I use from output variables? How?
There is an OUTPUT clause available, depending on your version; however, in this case it is probably easier (since you only have one row) to handle this separately:
IF #guidErae_ID IS NULL SET #guidErae_ID = NEWID()
then use #guidErae_ID "as is" in the INSERT (no ISNULL), and then either end with:
SELECT #guidErae_ID
and use var result = (Guid)command.ExecuteScalar(), or mark #guidErae_ID as OUTPUT in the parameter definition, and query it after calling command.ExecuteNonQuery().
If you need to return the value in the #guidErae_ID parameter that you've included, then you need to mark the parameter for output, and then set it in the proc:
ALTER PROCEDURE [dbo].[pr_Tbl_Erae_Insert]
#sfk_Dars varchar(20),
#sfk_Ostad varchar(20),
#byfk_Gerayesh tinyint,
#ifk_term int,
#guidErae_ID uniqueidentifier output,
#byGroup_Number tinyint,
AS
If #guidErae_ID is null set #guidErae_ID = newid()
-- INSERT a new row in the table.
INSERT [dbo].[Tbl_Erae]
(
[fk_Dars],
[fk_Ostad],
[fk_Gerayesh],
[fk_term],
[Erae_ID],
[Group_Number]
)
VALUES
(
#sfk_Dars,
#sfk_Ostad,
#byfk_Gerayesh,
#ifk_term,
#guidErae_ID,
#byGroup_Number
)
at the end of your stored procedure just add one more line like this
return select Erae_Id WHERE [fk_Dars] = #sfk_Dars
AND
[fk_Ostad] = #sfk_Ostad
AND
[fk_Gerayesh] = #byfk_Gerayesh
AND
[fk_term] = #ifk_term
AND
[Group_Number] = #byGroup_Number

Categories