How to use external variables in SQL - sql

I'm trying to write a stored procedure in SQL which goes like this ..
create PROCEDURE procedure_name
(
#Name varchar,
#Price int
)
AS
BEGIN
select * from table
where Name=#Name AND Price=#Price
END
When I call the stored procedure with values - John and 14228.. I get an empty table back. Is there something I'm doing wrong?
When I run the following snippet below, I get an output with rows.
select *
FROM table
WHERE Name = 'John' AND price = '14228';
Any help will be appreciated! thanks

The #Name variable needs to have a size specified. When you specify varchar without a size SQL Server interprets that as varchar(1). So only the first character of the parameter you pass to the stored procedure is used.
The #Name should have the same size as the size of the Name column. For exampe:
create PROCEDURE procedure_name
(
#Name varchar(30),
#Price int
)
...

Related

Insert results of a table into stored procedure as parameters

I have a stored procedure which inserts values into a table.
Let's say its name is usp_InsertTableA with parameters #ID int and Name varchar(100).
I have a requirement to call this stored procedure multiple times from another stored procedure. I am thinking to call this stored procedure something like below
exp usp_InsertTableA
select ID, Name from #tempTable
Is this possible in SQL Server to execute this with the value of the table and send it into a stored procedure?
You can use table type parameters to stored procedure.
CREATE TYPE [dbo].[udt_MyCustomTable] AS TABLE(
[id] [int] NOT NULL,
[name] [nvarchar](100) NOT NULL
)
GO
And then you stored procedure would be:
CREATE PROC [dbo].[usp_InsertTableA]
(
#myCustomTable udt_MyCustomTable READONLY
)
AS
BEGIN
-- Your code goes in here
END
Is this possible in SQL Server to execute this with the value of the table and send it into a stored procedure?
No, not with the stored procedure you have there. There are ugly hacks that could make it happen, but it's not how you're supposed to do things in T-SQL. Everything you do in SQL Server is supposed to be optimized to work on a set of rows, not a single row / row by row
In practice what this means is, if you have a query like this that produces 100 rows:
select ID, Name from #tempTable
You would pass those 100 rows to your insert procedure and insert them in one operation:
--expanding on sam's advice
--create a type
CREATE TYPE [dbo].[udt_MyCustomTable] AS TABLE(
[id] [int] NOT NULL,
[name] [nvarchar](100) NOT NULL
)
--your insert procedure
CREATE PROC [dbo].[usp_InsertTableA]
(
#myCustomTable udt_MyCustomTable READONLY
)
AS
BEGIN
INSERT INTO TableA(idcolumn, namecolumn)
SELECT is, name FROM #myCustomTable
END
Now in your main sp that wants to insert 100 rows:
#DECLARE tmpVar udt_MyCustomTable;
--put 100 rows into table variable
INSERT INTO tmpVar(id,name)
select ID, Name from #tempTable
--pass 100 rows in variable to SP to insert all at once
EXECUTE usp_InsertTableA tmpVar
DECLARE #ID INT, #Name VARCHAR(255)
SELECT #ID = ID, #Name=Name FROM #tempTable -- assumes one record in the table.
EXEC dbo.usp_insertdata #id, #Name

Select query not retrieving records when executed in stored procedure

I am having a doubt, can anyone please explain me why I am getting record in select query but not able to get the same when the query is executed from a stored procedure
Below ,select query retrieving records
and here no records for the same query in stored procedure
It seems your #name parameter of type varchar(25) is to small to fit the example name in the query. It would be truncated and the query would give no result.
As pointed out the name is more than varchar(25) and same may hold true for other values of mnthname as well. So alter funciton definition as:
Alter proc FindsSring
(
#name varchar(max),
#STD varchar(10),
#Div varchar(2),
#month varchar(100)
)
As ...

Run one stored procedure within another stored procedure in SQL Server 2008

I have a stored procedure named [usp_Movie_GetUserPaidList] that takes two arguments #MovieID INT, #UserName Nvarchar(250) and returns data something like this
Exec usp_Movie_GetUserPaidList #MovieID, #UserName
IsPaidUser | IsSubscribeUser
0 0
Now in my another stored procedure I have something like
DECLARE #tblTemp1 TABLE (
MovieID INT
,IsPaidUser BIT
,IsSubscribeUser BIT
)
Here I know #MovieID value
Now I need to do something like
INSERT INTO #tblTemp1
SELECT #MovieID (EXEC [usp_Movie_GetUserPaidList] #MovieID,#userName )
Which is obviously not correct.
Help me to do so...Thank you for your time.
You need to take output in another variable and then use it:
DECLARE #Movieid int
EXEC #Movieid = [usp_Movie_GetUserPaidList] #MovieID,#userName
Here assumptions are:
1. You have #MovieID and #userName with you.
2. Your stored proc usp_Movie_GetUserPaidList is returning proper value.
Why don't you return MovieID in the output of stored procedure usp_Movie_GetUserPaidList since you know it there also. Then you can use simple insert in another stored procedure like :
INSERT INTO #tblTemp1
EXEC [usp_Movie_GetUserPaidList] #MovieID,#userName

Get value from Another Stored procedure(B) in a stored Procedure(A)

I have a stored procedure ssspAccProfitAndLoss which returns result as shown below:
I have another stored procedure named ssspAccBalanceSheet. In this store procedure i have a variable declared as
Declare totalProfitAndLoss decimal(18,0)
I want the Total sum of Amount1 column of ssspAccProfitAndLoss and set it to totalProfitAndLoss. How can i achieve this.
Set totalProfitAndLoss = Select Sum(Amount1) from ssspAccProfitAndLoss
Thanxxxxx in advance....
If you don't want to change the ssspAccProfitAndLoss procedure you probably need to work through variable table - if the ssspAccProfitAndLoss is short running SP than it's probably acceptable.
declare #totalProfitAndLoss decimal(18,0); --I'd go for BIGINT here unless you're sure you need 18 digits
declare #resultTable table (
Particular varchar(100),
Amount decimal(18,0),
Particular2 varchar(100),
Amount2 decimal(18,0));
insert into #resultTable exec ssspAccProfitAndLoss
select #totalProfitAndLoss = sum(Amount1) from #resultTable

How to query from a stored procedure in SQL Server?

Let say I have a simple Stored Procedure:
ALTER PROCEDURE [dbo].[myProc]
AS
BEGIN
SELECT * FROM myTable
END
How can I do a WHERE statement in Microsoft SQL Server Management Studio to the stored procedure? Something like that:
SELECT * FROM myProc WHERE x = 'a'; -- But that doesn't work...
It sounds like you're trying to make a "dynamic" stored procedure.
Something you might want to do is:
1) Insert the contents of your stored procedure into a temporary table
2) Use dynamic sql to apply a where condition to that temporary table.
Something like:
declare #as_condition varchar(500); --Your condition
create table #a
(
id bigint
)
insert into #a
execute sproc
declare #ls_sql varchar(max);
set #ls_sql = "select * from #a where " + #as_condition;
execute (#ls_sql);
SQL Server allows you to use INSERT INTO to grab a stored procedure's output. For example, to grab all processes with SPID < 10, use:
create table #sp_who (
spid smallint,
ecid smallint,
status nchar(30),
loginame nchar(128),
hostname nchar(128),
blk char(5),
dbname nchar(128),
cmd nchar(16),
request int)
insert into #sp_who execute sp_who
select * from #sp_who where spid < 10
You can't add a WHERE clause to a stored procedure like this.
You should put the clause in the sproc, like this:
ALTER PROCEDURE [dbo].[myProc]
#X VARCHAR(10)
AS
BEGIN
SELECT * FROM myTable WHERE x=#X
END
GO
The syntax for calling a stored procedure is through the use of EXECUTE not SELECT(e.g.):
EXECUTE dbo.myProc 'a'
I think you can't do that.
The command to execute a stored procedure is EXECUTE.
See some more examples of the EXECUTE usage.
I think its better to use a view or a table valued function rather than the suggested approach. Both allow you to pass parameters to the function
If you want the WHERE clause to be something you can "turn off" you can do this, passing in a predetermined value (e.g. -1) if the WHERE limitation is to be bypassed:
ALTER PROCEDURE [dbo].[myProc]
#X VARCHAR(10)
AS
BEGIN
SELECT * FROM myTable WHERE x=#X or #X = -1
END
GO
You must declare a variable in the store procedure which will be necessary to pass to run the stored procedure. Here is an example. Keep this in mind: Before AS you can simply declare any variable by using the # character, but after the AS you must write Declare to declare any variable, e.g., Declare #name nvarchar (50).
ALTER PROCEDURE [dbo].[myProc]
#name varchar (50)
AS
BEGIN
SELECT * FROM myTable
where name= #name
END