How to call stored procedure with parameters in Yii2 - stored-procedures

How to call stored procedure with parameters in Yii2?
This my stored procedure
ALTER PROCEDURE [dbo].[usp_M_Inventory#InsertData]
#ID_Item RunNum,
#Item_Name varchar(250),
#ID_Mom varchar(50),
#Item_Price float,
#ID_Inv_Category RunNum,
#Item_PIC1 varchar(50),
#Item_PIC2 varchar(50),
#Item_active BIT,
#User UserDataType
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO M_Inventory(ID_Item, Item_Name, ID_Mom, Item_Price,
ID_Inv_Category, Item_PIC1, Item_PIC2,
Item_active, insert_user, insert_date, update_user, update_date)
VALUES (#ID_Item, #Item_Name, #ID_Mom, #Item_Price,
#ID_Inv_Category, #Item_PIC1, #Item_PIC2,
#Item_active, #User, GETDATE(), #User, GETDATE())
END

You can use below code to call stored procedure with parameters.
$result = \Yii::$app->db->createCommand("CALL storedProcedureName(:paramName1, :paramName2)")
->bindValue(':paramName1' , $param1 )
->bindValue(':paramName2', $param2)
->execute();

Related

Error: Query execution failed for dataset. Is my query correct for my dataset SSRS?

I'm trying to use SET,IF,ALTER TABLE. When i run the report it gives me a error 'Query execution failed for dataset'.
This is the query for the dataset:
declare #DOB date
declare #NO VARCHAR (30)
declare #ID VARCHAR(30)
set #DOB = #DOB
set #NO = #NO
set #ID =#ID
IF #DOB is null
begin
SELECT DISTINCT [Name]
into tempName
FROM [Patient]
where [No_]= #NO
or[Id No_] = #ID
END
else
IF #DH is null
begin
SELECT DISTINCT [Name]
into tempName
FROM [Patient]
where DATEOFBIRTH = #DOB
or [Id No_] = #ID
END
else
IF #ID is null
begin
SELECT DISTINCT [Name]
into tempName
FROM [Patient]
where [No_] = #NO
or DATEOFBIRTH = #DOB
end
ALTER TABLE tempName alter column Name varchar (30) NULL
INSERT INTO tempName (Name) values ('Nothing')
SELECT case Name
when '' then ''
when 'Nothing' then '*Nothing'
else Name
end Name
from tempName
order by [Name]
drop table tempName
Can someone help?
table "tempname" is an actual table or it's a temporary table? In the latter case you need to call it with #temptable

sql server stored proc returns only one row

I have an issue where only the last record in a csv file is written to the database by my stored procedure. I thought this might be related to the type of file (csv or text) because I have a comma delimited text file with only five records that will write all records, but when I used a csv file only the last record is written. I did read a post which said that this could be related to using a scalar variable in the stored proc but I don't think that this is right because when I use the text file it's still the same stored proc. Please advise.
Thanks!
here's the stored proc:
USE [SomeDB]
GO
/****** Object: StoredProcedure [dbo].[SaveUser] Script Date: 8/1/2016 9:25:24 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Alter PROCEDURE [dbo].[SaveUser]
-- Add the parameters for the stored procedure here
#PartnerID INT,
#SourceID INT,
#OrgSourcedIDs NVARCHAR(50),
#Role NVARCHAR(50),
#UserID NVARCHAR(50),
#GivenName NVARCHAR(50),
#FamilyName NVARCHAR(50),
#Email NVARCHAR(50),
#Grade NVARCHAR(50),
#Identifier NVARCHAR(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE #ReturnVal NVARCHAR(10)
DECLARE #IsTransfer Bit = 0
DECLARE #IsUpdate BIT = 0
SELECT #IsTransfer = CASE WHEN OrgSourcedIDs != #OrgSourcedIDs THEN 1 ELSE 0 END,
#IsUpdate = CASE WHEN HASHBYTES('SHA', GivenName +FamilyName +Email +Grade ) != HASHBYTES('SHA', #GivenName + #FamilyName + #Email + #Grade ) THEN 1 ELSE 0 END
FROM dbo.tblUsers a
WHERE a.PartnerID = #PartnerID AND a.SourcedID = #SourceID
IF ##RowCount = 0
BEGIN
-- If not, add it to staging table with ACTION = INSERT (into tblUser)
INSERT INTO dbo.tblUsers
( PartnerID, SourcedID, OrgSourcedIDs, Role, UserID,
GivenName, FamilyName, Email, Grade, Identifier,
Action )
VALUES
( #PartnerID, #SourceID, #OrgSourcedIDs, #Role, #UserID,
#GivenName, #FamilyName, #Email, #Grade, #Identifier,
'Create' )
--SELECT #ReturnVal = 'INSERT'
END
ELSE IF (#IsTransfer = 1)
BEGIN
UPDATE dbo.tblUsers
SET
OrgSourcedIDs = #OrgSourcedIDs,
UserID = #UserID,
GivenName = #GivenName,
FamilyName = #FamilyName,
Email = #Email,
Grade = #Grade,
Identifier = #Identifier,
Action = 'Transfer'
WHERE
PartnerID = #PartnerID
AND SourcedID = #SourceID
END
ELSE IF(#IsUpdate = 1)
BEGIN
UPDATE dbo.tblUsers
SET
UserID = #UserID,
GivenName = #GivenName,
FamilyName = #FamilyName,
Email = #Email,
Grade = #Grade,
Identifier = #Identifier,
Action = 'Update'
WHERE
PartnerID = #PartnerID
AND SourcedID = #SourceID
END
--SELECT #ReturnVal
END
GO

Am inserting values into four tables simultaneously using output parameter bt only one table is getting value

USE [ctsdev]
GO
/****** Object: StoredProcedure [dbo].[usp_insertemployeeinfo] Script Date: 03/24/2016 11:33:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_insertemployeeinfo]
#Emp_id int ,
#FirstName varchar(100),
#MiddleName varchar(50),
#LastName varchar(100),
#Date_of_join date,
#Job_title varchar(50),
#Grade varchar(50),
#Department varchar(50),
#Location varchar(100),
#Date_of_birth date,
#Nationality varchar(100),
#Mobile_no varchar(10),
#Psprt_yn varchar(1),
#Passport_no varchar(8),
#Date_of_issue date,
#Place_of_issue varchar(100),
#Date_of_expiry date,
#Pancard_yn varchar(1),
#Pancard_no varchar(10),
#Relatives_yn varchar(1),
#Relative_name varchar(100),
#Relative_grade varchar(100),
#Relative_dept varchar(100), /*1st table*/
#Home_adrs varchar(max),
#Home_number varchar(10),
#Home_mailid varchar(100),
#CHA_PHA_same varchar(1),
#Contact_name varchar(100),
#Relation varchar(100),
#City varchar(100),
#Contact_adrs varchar(100),
#Contact_num varchar(100), /*2nd table*/
#Qualification varchar(100),
#University_board varchar(100),
#Specialization varchar(100),
#Year_of_passing date,
#Grade_cgpa varchar(20), /*3rd table*/
#Bank_name varchar(100),
#Bank_adrs varchar(100),
#Account_num varchar(50),
#Ifsc_code varchar(50),
#Name_of_acnt_holder varchar(100), /*4th table*/
#From_date date,
#To_date date,
#Organization varchar(100),
#Job_title_his varchar(50),
#Sal_per_mnth varchar (50),
#Reasons_for_leavn varchar(max),
#Reference_name varchar(100),
#Contact_num_hist varchar(10),
#Organization_ref varchar(100),
#Job_title_ref varchar(50), /*5th table*/
#newid int output
AS
BEGIN
SET NOCOUNT ON
/* INSERTING INTO 1ST TABLE*/
INSERT INTO [tbl_empinfo ](FirstName,MiddleName,LastName,Date_of_join,Job_title,Grade,Department,Location,Date_of_birth,Nationality,Mobile_no,Psprt_yn,Passport_no,Date_of_issue, Place_of_issue,Date_of_expiry, Pancard_yn,Pancard_no,Relatives_yn, Relative_name, Relative_grade,Relative_dept )
VALUES (#FirstName,#MiddleName,#LastName,#Date_of_join,#Job_title,#Grade,#Department,#Location,#Date_of_birth,#Nationality,#Mobile_no,#Psprt_yn,#Passport_no,#Date_of_issue,#Place_of_issue,#Date_of_expiry,#Pancard_yn,#Pancard_no,#Relatives_yn,#Relative_name,#Relative_grade,#Relative_dept)
SELECT #Emp_id = SCOPE_IDENTITY()
SELECT #Emp_id As newid
RETURN
/*SELECT #identity=SCOPE_IDENTITY()*/
/* INSERTING INTO 2 TABLE*/
INSERT INTO tbl_address (Emp_id,Home_adrs,Home_number,Home_mailid,CHA_PHA_same,Contact_name,Relation,City,Contact_adrs,Contact_num)
VALUES (#Emp_id,#Home_adrs,#Home_number,#Home_mailid,#CHA_PHA_same,#Contact_name,#Relation,#City,#Contact_adrs,#Contact_num)
SELECT #Emp_id=SCOPE_IDENTITY()
RETURN
/* INSERTING INTO 3RD TABLE*/
INSERT INTO tbl_empeducation (Emp_id,Qualification,University_board,Specialization,Year_of_passing,Grade_cgpa)
VALUES (#Emp_id,#Qualification,#University_board,#Specialization,#Year_of_passing,#Grade_cgpa)
SELECT #Emp_id=SCOPE_IDENTITY()
RETURN
/* INSERTING INTO 4TH TABLE*/
INSERT INTO tbl_bank_account_dtls(Emp_id,Bank_name,Bank_adrs,Account_num,Ifsc_code,Name_of_acnt_holder)
VALUES(#Emp_id,#Bank_name,#Bank_adrs,#Account_num,#Ifsc_code,#Name_of_acnt_holder)
SELECT #Emp_id=SCOPE_IDENTITY()
RETURN
/* INSERTING INTO 5TH TABLE*/
INSERT INTO tbl_Employment_hist(Emp_id,From_date,To_date,Organization,Job_title_his,Sal_per_mnth,Reasons_for_leavn,Reference_name,Contact_num_hist,Organization_ref,Job_title_ref)
VALUES (#Emp_id,#From_date,#To_date,#Organization,#Job_title_his,#Sal_per_mnth,#Reasons_for_leavn,#Reference_name,#Contact_num_hist,#Organization_ref,#Job_title_ref)
SELECT #Emp_id=SCOPE_IDENTITY()
RETURN
/*SELECT #identity AS Id*/
END
GO
Your SP contains multiple RETURN statements, and it will exit after the first one. The remainder will not be executed.
Reference: https://msdn.microsoft.com/en-us/library/ms174998.aspx
RETURN (Transact-SQL)
Exits unconditionally from a query or procedure. RETURN is immediate and complete and can be used at any point to exit from a procedure, batch, or statement block. Statements that follow RETURN are not executed.

Multiple condition WHERE clause returning unnecessary rows

In the stored procedure below, I'm wanting to return rows that do not have DEL in the Flag column. When I get the result back, the DEL rows are included. What am I doing wrong in my WHERE clause(I'm assuming that's what it is)?
ALTER PROCEDURE [dbo].[GetAllMessages]
-- Add the parameters for the stored procedure here
#Key varchar(30)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT message.msg_id,
message.tenant_id,
message.sender_id,
message.recipient_id,
message.recipient_email,
message.description,
message.date_recorded,
message.filename,
message.size_bytes,
message.size_time,
message.filepath,
message.Flag,
message.title,
message.date_activity,
member.member_image_url
FROM message
INNER JOIN member
ON message.sender_id = member.person_id
WHERE sender_id = (SELECT person_id FROM auth_key WHERE key = #Key)
OR recipient_id = (SELECT person_id FROM auth_key WHERE key = #Key)
AND Flag != 'DEL'
END
I suspect you need some extra parens to segment the logic in the manner you want:
WHERE (sender_id = (SELECT person_id FROM auth_key WHERE key = #Key)
OR recipient_id = (SELECT person_id FROM auth_key WHERE key = #Key))
AND Flag != 'DEL'

How to apply update query with IN operator using stored procedure

Question:
update tablename
set columnname = "12"
where columnname2 in ('1','2','3','4');
It will run with simple query.
How to run above query using a stored procedure i.e
update tablename
set columname = #param1
where columnname2 in (#param2);
where #param2 have '1','2' etc..
You should create a function which is used to delimit your comma seperated String..
CREATE FUNCTION SplitString
(
#Input NVARCHAR(MAX),
#Character CHAR(1)
)
RETURNS #Output TABLE (
Item NVARCHAR(1000)
)
AS
BEGIN
DECLARE #StartIndex INT, #EndIndex INT
SET #StartIndex = 1
IF SUBSTRING(#Input, LEN(#Input) - 1, LEN(#Input)) <> #Character
BEGIN
SET #Input = #Input + #Character
END
WHILE CHARINDEX(#Character, #Input) > 0
BEGIN
SET #EndIndex = CHARINDEX(#Character, #Input)
INSERT INTO #Output(Item)
SELECT SUBSTRING(#Input, #StartIndex, #EndIndex - 1)
SET #Input = SUBSTRING(#Input, #EndIndex + 1, LEN(#Input))
END
RETURN
END
GO
Now your update Query should be
Update tablename set columname=#param1
Where columnname2 in (SELECT Item FROM dbo.SplitString(#param2, ','))

Resources