error line 86 for stored procedure - stored-procedures

I am trying to get this stored procedure to work. I have been working for a month straight, was up till 3 last night, and am approaching burnout. I really want to get this to work as we have an ad campaign that we spent hundreds on sending traffic to.
USE [redhotkitties2005db]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[proc_rhkprod_lock_items_for_paypal]
#cfuseridid bigint
,#transaction_guid uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
declare #tblcartID bigint
,#cfuserid bigint
,#tblproductsid bigint
,#quantity bigint
,#localInventoryID varchar(100)
,#taxologykey bigint
,#name varchar(1000)
,#shortDescription varchar(8000)
,#productDescription varchar(8000)
,#UorLorUL varchar(2)
,#unitPrice money
,#shippingWeight numeric(6, 2)
,#overSize tinyint
,#smallPic1 nvarchar(100)
,#largePic1 nvarchar(100)
,#smallPic2 nvarchar(100)
,#largePic2 nvarchar(100)
,#smallPic3 nvarchar(100)
,#largePic3 nvarchar(100)
SET #transaction_guid = coalesce(#transaction_guid,newid())
declare c1 cursor forward_only for
select rhkProd_tblCart.tablePK AS tblcartID
,rhkProd_tblProducts.productID as tblproductsid
,rhkProd_tblCart.quantity
,rhkProd_tblProducts.localInventoryID
,rhkProd_tblProducts.name
,rhkProd_tblProducts.taxologyKey
,rhkProd_tblProducts.shortDescription
,rhkProd_tblProducts.productDescription
,rhkProd_tblProducts.UorLorUL
,rhkProd_tblProducts.unitPrice
,rhkProd_tblProducts.shippingWeight
,rhkProd_tblProducts.overSize
,rhkProd_tblProducts.smallPic1
,rhkProd_tblProducts.largePic1
,rhkProd_tblProducts.smallPic2
,rhkProd_tblProducts.largePic2
,rhkProd_tblProducts.smallPic3
,rhkProd_tblProducts.largePic3
from rhkProd_tblCart
INNER JOIN rhkProd_tblProducts
on rhkProd_tblCart.tblProductsFK = rhkProd_tblProducts.productID
where rhkProd_tblCart = #cfuserid
open c1
fetch next
from c1
into #tblcartID
,#cfuserid
,#tblproductsid
,#quantity
,#localinventoryID
,#name
,#taxologykey
,#shortdescription
,#productdescription
,#UorLorUL
,#unitprice
,#shippingweight
,#oversize
,#smallpic1
,#largepic1
,#smallpic2
,#largepic2
,#smallpic3
,#largepic3
begin
while ##fetch_status = 0
insert into rhkprod_tblpaypallock (
,[cfuserid]
,[transaction_guid]
,[timestamp]
,[tblproductsFK]
,[quantity]
,[localInventoryID]
,[name]
,[taxologykey]
,[shortdescription]
,[productdescription]
,[uorlorul]
,[unitprice]
,[shippingweight]
,[oversize]
,[smallpic1]
,[largepic1]
,[smallpic2]
,[largepic2]
,[smallpic3]
,[largepic3]
)
values (
,#cfuserid
,#transaction_guid
,getdate()
,#tblproductsid
,#quantity
,#localinventoryID
,#name
,#taxologykey
,#shortdescription
,#productdescription
,#UorLorUL
,#unitprice
,#shippingweight
,#oversize
,#smallpic1
,#largepic1
,#smallpic2
,#largepic2
,#smallpic3
,#largepic3
)
update rhkprod_tblproducts
set quantity = quantity - #quantity
where productid = #tblproductsid
and UorLorUL in ('U','L')
fetch next
from c1
into #tblcartID
,#cfuserid
,#tblproductsid
,#quantity
,#localinventoryID
,#name
,#taxologykey
,#shortdescription
,#productdescription
,#UorLorUL
,#unitprice
,#shippingweight
,#oversize
,#smallpic1
,#largepic1
,#smallpic2
,#largepic2
,#smallpic3
,#largepic3
close c1
deallocate c1
end
END

Your INSERT statement has a stray comma at the start:
insert into rhkprod_tblpaypallock (
,[cfuserid]
Should be
insert into rhkprod_tblpaypallock (
[cfuserid]
Your VALUES clause also has a stray comma:
values (
,#cfuserid
Should be
values (
#cfuserid

Related

stored procedure for inserting values of tables into another tables with automatic identifier as FK PGadmin4

I want to make an insert procedure for specific values of an table into another existing table.
The dificulty is that my excisting table has automatic identifiers as fk of another table.
When I insert them individually, I use a look up function. But now I don't known in PGadmin how to put this in a procedure. In oarcle I can use collections but in pgadmin I can't figure it out.
my tables:
CREATE TABLE public.field (field_id INT GENERATED BY DEFAULT AS IDENTITY
CONSTRAINT pk_veld_id PRIMARY KEY,
object_number INT,
experiment_number INT,
repetition INT,
plants_field INT,
stem_field INT,
plants_square_meter numeric,
stem_square_meter_start numeric,
stem_square_meter_end numeric,
date_extra_stem date,
row_number_field INT);
CREATE TABLE public.sticky_plates_fields (ID_sticky_plate INT GENERATED BY DEFAULT AS IDENTITY
CONSTRAINT pk_sticky_plate_id PRIMARY KEY,
sticky_plate_number INT,
brand_sticky_plate varchar,
version_plate numeric,
field_id INT constraint fk_sticky_plates_fields references field );
ALTER TABLE IF EXISTS public.sticky_plates_fields
ADD CONSTRAINT sticky_plates_fields_unique UNIQUE (sticky_plate_number, field_id);
DROP TABLE IF EXISTS public.make_sticky_plate_counts CASCADE;
CREATE TABLE public.make_sticky_plate_counts (
experiment_number INT,
object_number INT,
repetition INT,
sticky_plate_number INT,
brand_sticky_plate varchar,
version_plate numeric,
date_start date,
date_count date,
species varchar,
stage varchar,
count_species INT);
look_up _function:
CREATE OR REPLACE FUNCTION project.lookup_field_id
(p_objectnumber INT,
p_experimentnumber INT,
p_repetition INT)
RETURNS integer
LANGUAGE 'plpgsql'
AS
$BODY$
DECLARE
ln_field_id integer;
BEGIN
SELECT field_id INTO ln_field_id
FROM field
WHERE p_objectnumber = object_number AND p_experimentnumber = experiment_number AND p_repetition = repetition;
RETURN ln_field_id;
END;
$BODY$;
values:
insert into public.field(object_number,experiment_number,repetition,stem_field,stem_square_meter_start,stem_square_meter_end,date_extra_stem,row_number_field)
values (1,4072022,1,20,2.5,3.3,TO_DATE('1-04-2022','DD-MM-YYYY'),10);
insert into public.field(object_number,experiment_number,repetition,stem_field,stem_square_meter_start,stem_square_meter_end,date_extra_stem,row_number_field)
values (1,4072022,2,20,2.5,3.3,TO_DATE('1-04-2022','DD-MM-YYYY'),15);
insert into public.field(object_number,experiment_number,repetition,stem_field,stem_square_meter_start,stem_square_meter_end,date_extra_stem,row_number_field)
values (1,4072022,3,20,2.5,3.3,TO_DATE('1-04-2022','DD-MM-YYYY'),20);
insert into public.field(object_number,experiment_number,repetition,stem_field,stem_square_meter_start,stem_square_meter_end,date_extra_stem,row_number_field)
values (1,4072022,4,20,2.5,3.3,TO_DATE('1-04-2022','DD-MM-YYYY'),25);
insert into public.field(object_number,experiment_number,repetition,stem_field,stem_square_meter_start,stem_square_meter_end,date_extra_stem,row_number_field)
values (2,4072022,1,20,2.5,3.3,TO_DATE('1-04-2022','DD-MM-YYYY'),10);
insert into public.field(object_number,experiment_number,repetition,stem_field,stem_square_meter_start,stem_square_meter_end,date_extra_stem,row_number_field)
values (2,4072022,2,20,2.5,3.3,TO_DATE('1-04-2022','DD-MM-YYYY'),15);
insert into public.field(object_number,experiment_number,repetition,stem_field,stem_square_meter_start,stem_square_meter_end,date_extra_stem,row_number_field)
values (2,4072022,3,20,2.5,3.3,TO_DATE('1-04-2022','DD-MM-YYYY'),20);
insert into public.sticky_plates_fields(sticky_plate_number,brand_sticky_plate,version_plate,field_id)
values(2,'BIOBEST',3,project.lookup_field_id(1,4072022,2));
insert into public.sticky_plates_fields(sticky_plate_number,brand_sticky_plate,version_plate,field_id)
values(1,'BIOBEST',3,project.lookup_field_id(1,4072022,1));
insert into public.sticky_plates_fields(sticky_plate_number,brand_sticky_plate,version_plate,field_id)
values(3,'BIOBEST',3,project.lookup_field_id(1,4072022,3));
insert into public.sticky_plates_fields(sticky_plate_number,brand_sticky_plate,version_plate,field_id)
values(4,'BIOBEST',3,project.lookup_field_id(1,4072022,4));
insert into public.make_sticky_plate_counts(experiment_number,object_number,repetition,sticky_plate_number,brand_sticky_plate,version_plate,date_start,date_count,species,stage,count_species)
values(4072022,2,1,6,'BIOBEST',2.1,TO_DATE('1-04-2022','DD-MM-YYYY'),TO_DATE('14-04-2022','DD-MM-YYYY'),'WHITE_FLY_T','ADULT',12) ;
insert into public.make_sticky_plate_counts(experiment_number,object_number,repetition,sticky_plate_number,brand_sticky_plate,version_plate,date_start,date_count,species,stage,count_species)
values(4072022,2,2,7,'BIOBEST',2.1,TO_DATE('1-04-2022','DD-MM-YYYY'),TO_DATE('14-04-2022','DD-MM-YYYY'),'WHITE_FLY_T','ADULT',12) ;
insert into public.make_sticky_plate_counts(experiment_number,object_number,repetition,sticky_plate_number,brand_sticky_plate,version_plate,date_start,date_count,species,stage,count_species)
values(4072022,2,3,8,'BIOBEST',2.1,TO_DATE('1-04-2022','DD-MM-YYYY'),TO_DATE('14-04-2022','DD-MM-YYYY'),'WHITE_FLY_T','ADULT',12) ;
insert into public.make_sticky_plate_counts(experiment_number,object_number,repetition,sticky_plate_number,brand_sticky_plate,version_plate,date_start,date_count,species,stage,count_species)
values(4072022,2,4,9,'BIOBEST',2.1,TO_DATE('1-04-2022','DD-MM-YYYY'),TO_DATE('14-04-2022','DD-MM-YYYY'),'WHITE_FLY_T','ADULT',12) ;
try out of stored procedure
Here I want some values of table make_sticky_plate_counts to insert into table sticky_plates_fields.
I don't know how I can make a procedure to insert the whole (distinct table) winto the sticky plate field table and using the look up function for finding the related FK integer.
CREATE OR REPLACE PROCEDURE insert_records
() LANGUAGE 'plpgsql'
AS
$BODY$
DECLARE
p_object_number INT;
p_experiment_number INT;
p_r_epetitition INT;
p_sticky_plate_number INT;
p_brand_sticky_plate VARCHAR;
p_version_plate VARCHAR;
max_rownumbers_insert INT := 0;
BEGIN
max_rownumbers_insert := SELECT COUNT(*) FROM (SELECT DISTINCT object_number,experiment_number,repetition FROM make_sticky_plate_counts) as temp;
FOR i IN 1..max_rownumbers_insert
LOOP
p_object_number := SELECT object_number [i] FROM (SELECT DISTINCT object_number,experiment_number,repetition FROM make_sticky_plate_counts) as temp ;
p_experiment_number := SELECT experiment_number [i] FROM (SELECT DISTINCT object_number,experiment_number,repetition FROM make_sticky_plate_counts) as temp ;
p_repetitition:= SELECT repetitition [i] FROM (SELECT DISTINCT object_number,experiment_number,repetition FROM make_sticky_plate_counts) as temp ;
p_sticky_plate_number:=SELECT sticky_plate_number [i] FROM (SELECT DISTINCT object_number,experiment_number,repetition FROM make_sticky_plate_counts) as temp);
p_brand_sticky_plate :=SELECT brand_sticky_plate [i] FROM (SELECT DISTINCT object_number,experiment_number,repetition FROM make_sticky_plate_counts) as temp);
p_version_plate :=SELECT version_plate [i] FROM (SELECT DISTINCT object_number,experiment_number,repetition FROM make_sticky_plate_counts) as temp);
INSERT INTO sticky_plate_fields(field_id, sticky_plate_number, brand_sticky_plate,version_plate)
VALUES (project.lookup_field_id(p_object_number,p_experiment_number,p_repetition),p_sticky_plate_number,p_brand_sticky_plate,p_version_plate);
END LOOP; ```
I could figure it out. Maybe it is helpfull for someone else:
CREATE OR REPLACE PROCEDURE insert_records
() LANGUAGE 'plpgsql'
AS
$BODY$
DECLARE
curs cursor for select * FROM (SELECT DISTINCT object_number,experiment_number,repetition,sticky_plate_number,brand_sticky_plate,version_plate FROM make_sticky_plate_counts) as temp;
BEGIN
FOR row IN curs LOOP
INSERT INTO sticky_plates_fields(field_id, sticky_plate_number, brand_sticky_plate,version_plate)
VALUES (project.lookup_field_id(row.object_number,row.experiment_number,row.repetition),row.sticky_plate_number,row.brand_sticky_plate,row.version_plate);
END LOOP;
END ;
$BODY$;

Dynamic Creation of External Tables in Synapse

I have a json string which is the result of Get Metadata activity in data factory in the following format: {"structure": [{"name": "Id","type": "String"},{"name": "IsDeleted","type": "Boolean"},{"name": "RowVersion","type": "Byte[]"}, {"name": "ModelId","type": "String"}]
This json is showing the schema of parquet file. I pass this json along with the parquet filename and uctnow() to a stored procedure to create a table of schema for that file with the following code
CREATE OR ALTER PROCEDURE [CreateExternalTables] (
#schema NVARCHAR (MAX), #tableName NVARCHAR(MAX), #ExecTime NVARCHAR(MAX)
) AS
BEGIN
IF OBJECT_ID('tempdb..#tables_to_create', 'U') IS NOT NULL
DROP TABLE #tables_to_create
CREATE TABLE #tables_to_create (
tableName NVARCHAR (MAX),
fieldOrder NVARCHAR (MAX),
fieldName NVARCHAR (MAX),
fieldType NVARCHAR (MAX),
translatedType NVARCHAR (MAX),
executeTime NVARCHAR (MAX)
)
BEGIN
WITH Fields (fieldOrder, fieldName, fieldType) AS (
SELECT
[key] AS fieldOrder,
JSON_VALUE([value], '$.name') AS fieldName,
JSON_VALUE([value], '$.type') AS fieldType
FROM
OPENJSON(#schema)
)
INSERT INTO
#tables_to_create(
tableName,
fieldOrder,
fieldName,
fieldType,
translatedType,
executeTime
)
SELECT
#tableName,
fieldOrder,
fieldName,
fieldType,
CASE
WHEN fieldType = 'Single' THEN 'real'
WHEN fieldType = 'Boolean' THEN 'bit'
WHEN fieldType = 'Double' THEN 'float'
WHEN fieldType = 'Int64' THEN 'bigint'
ELSE NULL
END AS translatedType,
#ExecTime
FROM
Fields
END
END;
The overall setup is like below:
But I receive the following error which I'm not sure why:
What I would like to do is to create this temo table in stored procedure in order to create external tables in a automatic way.
To be precise I'm following this link to do it:
create-external-dataset
Any help is highly appreciated.
UPDATE
After solving the first problem with #wBob 's help I follow the rest of the link to create the external tables with the following which I inserted into my stored procedure:
FROM
Fields
END
Declare #sqlCommand nvarchar(max);
Declare #folderPath nvarchar(max);
SET
#sqlCommand = 'IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].[' + #tableName + ']'') AND type in (N''U''))
CREATE EXTERNAL TABLE [dbo].[' + #tableName + '] ('
WHILE((SELECT COUNT(*) FROM #tables_to_create) > 0)
BEGIN
DECLARE #key int
SELECT
#key = MIN(fieldOrder)
FROM
#tables_to_create
WHERE
executeTime = #ExecTime
DECLARE #fieldName VARCHAR(50)
DECLARE #translatedType VARCHAR(50)
SELECT
#fieldName = fieldName,
#translatedType = translatedType
FROM
#tables_to_create
WHERE
fieldOrder = #key
AND executeTime = #ExecTime
SET
#sqlCommand = #sqlCommand + '
[' + #fieldName + '] [' + #translatedType + '] NULL'
DELETE FROM
#tables_to_create
WHERE
fieldOrder = #key
AND executeTime = #ExecTime
IF((SELECT COUNT(*) FROM #tables_to_create WHERE executeTime = #ExecTime) > 0)
SET
#sqlCommand = #sqlCommand + ', '
END
SET
#sqlCommand = #sqlCommand + '
)
WITH
(
LOCATION = ''/' + /main/json/ + ''',
DATA_SOURCE = DataLakeStaged,
FILE_FORMAT = StagedParquet
)'
EXEC(#sqlCommand)
END;
However I receive the following error:
As per the error message, queries are not supported in the conditions of WHILE statement in Azure Synapse Analytics. Dedicated SQL pools are MPP systems that have some slight differences from box-product SQL Server and Azure SQL DB and more generally, loops are a pattern that don't translate that well. You should consider creating a stored procedure that does not loop and leave any looping required to the For Each activity in Azure Data Factory (ADF) / Synapse Pipelines which can loop in parallel.
If you do require a loop, there are multiple examples of doing loops in dedicated SQL pools online, but there is a good example in the official documentation, reproduced here in the event the link moves:
-- First, create a temporary table containing a unique row number used to identify the individual statements:
CREATE TABLE #tbl
WITH
( DISTRIBUTION = ROUND_ROBIN
)
AS
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS Sequence
, [name]
, 'UPDATE STATISTICS '+QUOTENAME([name]) AS sql_code
FROM sys.tables
;
-- Second, initialize the variables required to perform the loop:
DECLARE #nbr_statements INT = (SELECT COUNT(*) FROM #tbl)
, #i INT = 1
;
-- Now loop over statements executing them one at a time:
WHILE #i <= #nbr_statements
BEGIN
DECLARE #sql_code NVARCHAR(4000) = (SELECT sql_code FROM #tbl WHERE Sequence = #i);
EXEC sp_executesql #sql_code;
SET #i +=1;
END

How to Execute Macro through Stored Procedure

I have created one macro in Teradata, now wish to call that macro through Stored Procedure (in Teradata only). The SQL part belongs to macro.
I wrote this procedure in case if execution of macro is not possible through procedure.
Kindly suggest both the option.
CREATE PROCEDURE MDM_STAGE.match_sqls_proc
(
in fname VARCHAR(30),
in match_frst_name VARCHAR (100),
in lname VARCHAR(30),
in match_last_name VARCHAR (100),
in addr1 VARCHAR(1000),
in zip_cd_base varchar(10),
in email VARCHAR(30),
in phone VARCHAR(30),
out msgs VARCHAR(10)
-- INOUT errstr VARCHAR(30)
)
begin
DECLARE SQLTEXT2 VARCHAR (10) ;
DECLARE TBL_COUNT INT ;
select count (*) into :TBL_COUNT from
(
SELECT
CUST.CUST_ID,
CUST.FRST_NAME,
CUST.MATCH_FRST_NAME,
CUST.LAST_NAME,
CUST.MATCH_LAST_NAME,
CUST.GNDR_TYPE_CD,
STREET_ADDR.ADDR_LN_1_TXT,
STREET_ADDR.ADDR_LN_2_TXT,
STREET_ADDR.ADDR_LN_3_TXT,
cast (coalesce (STREET_ADDR.ADDR_LN_1_TXT,'') || ' ' || coalesce
(STREET_ADDR.ADDR_LN_2_TXT,'' ) as varchar (1000)) as addr,
STREET_ADDR.CITY_NAME,
STREET_ADDR.POSTL_CD,
STREET_ADDR.STREET_ADDR_ID,
STREET_ADDR.ADDR_SBTYPE_CD,
ELCTRNC_ADDR.ELCTRNC_ADDR_ID,
ELCTRNC_ADDR.ELCTRNC_ADDR_SBTYPE_CD,
ELCTRNC_ADDR.ELCTRNC_ADDR_TXT,
TLPHN_NUM.TLPHN_LN_NUM,
TLPHN_NUM.TLPHN_NUM_ID
FROM
MDM_STAGE.REF_CUST_V CUST
LEFT OUTER JOIN
MDM_STAGE.REF_STREET_ADDR_V STREET_ADDR
ON
CUST.CUST_ID = STREET_ADDR.CUST_ID
AND
--RDM_ADDRSIM (STREET_ADDR.ADDR_LN_1_TXT ,'2285Main Street' ) =1
RDM_ADDRSIM ( cast (coalesce (STREET_ADDR.ADDR_LN_1_TXT,'') || ' ' ||
coalesce (STREET_ADDR.ADDR_LN_2_TXT,'' ) as varchar (1000)) ,:addr1) =1
AND
SUBSTR(STREET_ADDR.POSTL_CD,0,6) = (:zip_cd_base)
LEFT OUTER JOIN
MDM_STAGE.REF_ELCTRNC_ADDR_V ELCTRNC_ADDR
ON
CUST.CUST_ID = ELCTRNC_ADDR.CUST_ID
/*AND
STREET_ADDR.ADDR_SBTYPE_CD = ELCTRNC_ADDR.ELCTRNC_ADDR_SBTYPE_CD*/
AND
ELCTRNC_ADDR.ELCTRNC_ADDR_TXT= (:email)
LEFT OUTER JOIN
MDM_STAGE.REF_TLPHN_NUM_V TLPHN_NUM
ON
CUST.CUST_ID = TLPHN_NUM.CUST_ID
/*AND
STREET_ADDR.ADDR_SBTYPE_CD = TLPHN_NUM.ADDR_SBTYPE_CD*/
AND
TLPHN_NUM.TLPHN_LN_NUM = (:phone)
WHERE
(RDM_sndx(COALESCE(CUST.Match_FRST_NAME,CUST.CUST_ID))=RDM_sndx(:match_frst_name)
AND
RDM_sndx(COALESCE(STRTOK(CUST.Match_LAST_NAME,' ',1),CUST.CUST_ID))=RDM_SNDX(:match_last_name)
)
AND
(
TLPHN_NUM.CUST_ID IS NOT NULL
OR
ELCTRNC_ADDR.CUST_ID IS NOT NULL
OR
STREET_ADDR.CUST_ID IS NOT NULL
) A
;
IF ( TBL_COUNT > 0 ) THEN SET SQLTEXT2 = 'Match' ;
ELSE
SET Msgs = 'Non-Match' ;
END IF;
end;
Error message -
SPL1027:E(L88), Missing/Invalid SQL statement'E(3707):Syntax error, expected something like an 'EXCEPT' keyword or an 'UNION' keyword or a 'MINUS' keyword between ')' and the word 'A'.'.
No idea, what to add between the word 'A' and ')'. Tried all the possibility but not successful. Not sure how to pass the value of SQL into the 'count' as later call procedure condition is based on that count only.

Output parameter issue in a Store procedure

I have 2 table's Customer and Beneficiary. There are 2 or more Beneficiary added with a single Customer Mobile number.
I have Created a Stored proc to get the Beneficiary name related to that Mobile Number
create Proc Customer
(
#Mobile bigint,
#Beneficiary varchar(100) out,
#Beneficiary2 varchar(100) out
)
as
Begin
Select #Beneficiary = Beneficiary_Name, #Beneficiary2 = Beneficairy_Name from Beneficiary_master
as a
join Customer_Master as b
on a.CustomerID= b.CustomerID
where Mobile_Number = #Mobile
End
Declare #Beneficiary varchar(100)
Declare #Beneficiary2 varchar(100)
Exec spCustomer 999999999, #Beneficiary out, #beneficiary2 out
How to get both the Beneficiary Names, as i am getting only 1 name
Your Select Query will return the record(s) matching the select statement, and will assign the result of the last record to the variables #Beneficiary and #Beneficiary2
You have to change the select query to get the desired o/p. Please have a try on this statement, and if suit's your requirement make change in SP accordingly
Declare #Mobile bigint =123
Declare #Beneficiary varchar(100)
Declare #Beneficiary2 varchar(100)
Select #Beneficiary = Beneficiary_Name
from Beneficiary_master as a
join Customer_Master as b on a.CustomerID= b.CustomerID
where Mobile_Number = #Mobile
ORDER BY Beneficiary_Name ASC
OFFSET 0 ROWS
FETCH NEXT 1 ROWS ONLY
Select #Beneficiary2 = Beneficiary_Name
from Beneficiary_master as a
join Customer_Master as b on a.CustomerID= b.CustomerID
where Mobile_Number = #Mobile
ORDER BY Beneficiary_Name ASC
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY
Select #Beneficiary Beneficiary, #Beneficiary2 Beneficiary2

How to read output value from stored procedure in c#?

Here is my stored procedure:
ALTER PROCEDURE sp_searchupdate
(
#id int,
#id_student int,
#output varchar(50) output,
#Tamil Varchar (100),
#English varchar (50),
#Maths Varchar (50),
#Science Varchar (50),
#SocialScience Varchar (50)
)
AS
IF EXISTS (SELECT * FROM studentresult WHERE id=#id)
BEGIN
UPDATE studentresult SET Tamil = #Tamil,English = #English, Maths = #Maths,Science = #Science,SocialScience = #SocialScience WHERE id = #id
SET #output='Updated'
RETURN
END
Else
BEGIN
IF EXISTS (SELECT * FROM student WHERE id=#id_student)
SET #output='EXIST'
RETURN
BEGIN
INSERT into studentresult (id_student,Tamil,English,Maths,Science,SocialScience) values (#id_student,#Tamil,#English,#Maths,#Science,#SocialScience)
SET #output='Inserted'
RETURN
END
END
If inserted data in front-end it need to shows 'inserted' or update means 'updated' or else 'exist'.
But for the above query didn't show notification message.
May i know, what my mistake in my code?
Can anyone guide me, I'M new stored procedure and .net.
Thanks,
It's rather unclear exactly what you are trying to achieve - why would you only insert into the studentresult record if the student record did NOT exist? Anyway, currently if no studentresult record exists then the procedure will always return after the check for the student record. There is no BEGIN...END after the IF EXISTS so only the first statement after the IF will be dependent on the result. Try:
ALTER PROCEDURE sp_searchupdate
(
#id int,
#id_student int,
#output varchar(50) output,
#Tamil Varchar (100),
#English varchar (50),
#Maths Varchar (50),
#Science Varchar (50),
#SocialScience Varchar (50)
)
AS
BEGIN
IF EXISTS (SELECT * FROM studentresult WHERE id=#id)
BEGIN
UPDATE studentresult SET Tamil = #Tamil,English = #English, Maths = #Maths,Science = #Science,SocialScience = #SocialScience WHERE id = #id
SET #output='Updated'
END
Else
BEGIN
IF EXISTS (SELECT * FROM student WHERE id=#id_student)
BEGIN
SET #output='EXIST'
END
ELSE
BEGIN
INSERT into studentresult (id_student,Tamil,English,Maths,Science,SocialScience) values (#id_student,#Tamil,#English,#Maths,#Science,#SocialScience)
SET #output='Inserted'
END
END
RETURN
END

Resources