Select query not retrieving records when executed in stored procedure - sql

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 ...

Related

Select specific columns from the stored procedure

I have a stored procedure in other database which is maintained by other team. Assume that it is currently returning 3 columns, and my system only needs those 3 columns
but the other team can add few more columns for their own use which is causing my system to fail.
Other database SP
ALTER PROCEDURE FirstSP
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #A (Id INT, Name VARCHAR(200), Amount VARCHAR(100), TestColumn INT)
INSERT INTO #A VALUES
(1,'ABC','23',1), (2,'CDF','35454',2), (3,'hjhj','9809909',3)
SELECT * FROM #A
DROP TABLE #A
END
GO
And below is my query, which was only expecting 3 columns from the source
CREATE TABLE #MyTable (Id INT, Name VARCHAR(200), Amount INT)
INSERT INTO #MyTable
EXEC dbo.FirstSP;
SELECT * FROM #MyTable
DROP TABLE #MyTable
Is there any way I can provide the column list?
This is what I am trying but it seems that I can't use server name as the parameter
DECLARE #ServerName VARCHAR(100) = ##SERVERNAME
SELECT * FROM OPENQUERY(#ServerName,'EXEC dbo.FirstSP')
My whole problem is to just select required columns from the SP. SP can have many columns in future.
Try this:
/*
-----------------------------------------------------------------------
Replace <SERVERNAME>\<INSTANCENAME>], <DATABASENAME> with your names
*/ ------------------------------------------------------------------------
-- First, enable Data Access (One time only)
EXEC sp_serveroption '<SERVERNAME>\<INSTANCENAME>', 'DATA ACCESS', TRUE;
-- Then SELECT just the fields you need
SELECT ID, Name, Amount
FROM OPENQUERY([<SERVERNAME>\<INSTANCENAME>], 'EXEC <DATABASENAME>.dbo.FirstSP')
I would ask the team that develops the stored procedure to create a parameter "Team" or something and slightly change the sp so that it will return the expected columns based on this parameter.
A more cumbersome solution is to use this stored procedure the get the colum names of the (first) result returned by the sp.
sp_describe_first_result_set 'dbo.usp_mySp';
And then use the result to create some dynamic SQL.

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

How to use external variables in 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
)
...

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