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

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

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.

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

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

How to know total record return stored procedure in sql?

I have a stored procedure called usp_getTotalOrder which looks like
Select * from Order where CompanyID = 1;
Now, I have a table which contains stored procedure names.
In my BLL I have a stored procedure name. I want to create function which contain one parameter StoredProcedureName and returns the count of total rows which is like
Declare #str varchar(50)
Set #str='GetOrders'; // Stored Procedure Name
Exec #str
But it does not return total row count that I want to get from the stored procedure with its name in the function.
Any IDEA???
Please help.....
Take a look at ##ROWCOUNT
http://technet.microsoft.com/en-us/library/ms187316.aspx
You might be able to use something like:
Declare #str varchar(50)
Set #str='GetOrders'; // Stored Procedure Name
Exec #str
SELECT ##ROWCOUNT
Do you want to count the number of rows in the result set from the sproc?
In that case you could do:
INSERT INTO #Orders
Exec #str
SELECT COUNT(*)
FROM #Orders
or you could use a Table variable instead of the temp table.
You can return the count of rows by output parameter or return value from your SP. Of course you have to add that output parameter to your SP and set it with COUNT(*) inside the SP.
DECLARE #o_count INT
EXEC GetOrder #o_count = #count OUT