How to select query result inside stored procedure - sql

I'm writing a stored procedure in SQL Server 2008 which contains a SELECT DISTINCT statement and another simple Select statement which is based on the result of first statement.
How to use the table returned by the SELECT DISTINCT statement i.e the UnitNumber column value in second Select statement?
Stored procedure:
CREATE PROCEDURE ExtractPacket
AS
BEGIN
SET NOCOUNT ON;
-- Select statements to check the number of unit
SELECT DISTINCT UnitNumber from dbo.CP_TemplateHandler
END
GO

you can create a temp table and fill it by first SELECT DISTINCT and then in second Select use that.
Excuse me for answer in order comment(i can comment yet :( )

I suggest that use First Select Distinct as a sub query of second Select Distinct query.

Related

Stored procedure has multiple SELECT statements

This stored procedure has multiple SELECT statements - I need to write a condition based on one SELECT statement results among them without altering the stored procedure:
CREATE PROC Test
AS
BEGIN
SELECT 1 AS ID
FROM EMPLOYEE
SELECT NAME, ADDRESS, STATE
FROM EMPLOYEE --NEED TO TAKE ROW COUNT
END
After executing the stored procedure, I'll get 2 result sets. But I need to row count of 2nd select statement, without altering the stored procedure.
Based on that rowcount, I need to work further.
exec Test
select ##rowcount
In this case, ##rowcount will always return last result set row count.

Execute a stored procedure from within another stored procedure's SELECT statement?

I would like to execute a stored procedure X from within the SELECT statement of stored procedure Y, so that X's value can be returned as part of Y's data.
I am trying the following syntax, but it's apparently not valid.
SELECT name, type, (EXEC X #type=type)
FROM table
As I hope you can see above, I need to pass the current row's type value to procedure X to get the proper return value.
Disclaimer: I probably just don't know what I'm doing.
The approach what you have tried is invalid. Instead of the X as the stored procedure convert it as user-defined function. like the below
Create function dbo.fnGetTypeDetail
(
#type varchar(50)
)
returns varchar(100)
As
Begin
return --do your operation;
End
And replace your query as:
SELECT name, type, dbo.fnGetTypeDetail(type) AS TypeDetail
FROM table
For sample, I created a scalar function. Based on your requirement you can create inline table valued function as per the example
You can't EXEC a stored proc inside a SELECT statement.
What you can do is INSERT..EXEC a stored proc into a temp table, and then run a SELECT statement that queries that temp table, while joining to other tables if desired.
Psuedo-example:
INSERT INTO #Tmp (Column1) EXEC X;
SELECT Name, Type, (SELECT Column1 FROM #tmp)
FROM MyTable

Get count of records of a table which return from stored procedure

I have a stored procedure, which is returning a table. I just want the count of the records is it possible
My Procedure
create procedure Test
as begin
select * From Student
end
exec Test
will give the records out put
I want the count
NB: I need the sp to return the results of select statement.In another place I need the count of the records returned by sp and columns in the student table is dynamic.
I am expecting an answer without modifying stored procedure.
You can select the data into a temporary table like below. However, you have to use OPENQUERY to do so. You must also enable data access on your server first.
Exec sp_serveroption 'ServerName', 'data access', 'true'
SELECT * INTO #TempTable
FROM OPENQUERY("ServerName", 'EXEC Test')
SELECT COUNT(*) FROM #TempTable
NB: I need the sp to return the results of select statement.
Use your stored procedure as is. That is,
create procedure Test
as begin
select * From Student
end
In another place I need the count of the records returned by sp and
columns in the student table is dynamic.
If this other place is another SP, then use rowcount. You use it this way :
EXEC [sp_WhateverTheSPNameIs]
select ##rowcount
Read about ##RowCount. This is all you need.

I am trying to run a query based on the results from a stored procedure

First, here is the code for sp_GetWorkQByUserName:
ALTER PROCEDURE [dbo].[sp_GetWorkQByUserName]
( #UserName varchar(50),
#StartDate datetime,
#EndDate datetime )
AS
BEGIN
SET NOCOUNT ON;
SELECT DISTINCT SpotId FROM tblSpotCount WHERE StoreNum = EXECUTE sp_GetUserLocationCodes(#UserName)
ORDER BY SpotDt ASC
END
I know my SELECT DISTINCT statement is wrong, but I wrote it like that to help show what I'm trying to do. I want to run this stored procedure based on the results from the sp_GetUserLocationCodes with a parameter of #UserName.
From what I can tell, my problem lies in how I'm calling sp_GetUserLocationCodes.
Question: how can I run a SELECT DISTINCT query on tblSpotCount.SpotId based on the results from the sp_GetUserLocationCodes stored procedure?
You cannot use a stored procedure directly in a query. You can, however, insert the results of a stored procedure into a temporary table and use that in your query:
CREATE TABLE #storeLocations
(
-- appropriate column names and data types go here
)
INSERT INTO #storeLocations (put column list here)
EXECUTE sp_GetUserLocationCodes(#UserName)
SELECT DISTINCT SpotId
FROM tblSpotCount
WHERE EXISTS (SELECT 1
FROM #storeLocations
WHERE #storeLocations.StoreNum = tblSpotCount.StoreNum)
ORDER BY SpotDt ASC
DROP TABLE #storeLocations

How to combine stored procedure and select query result?

I am trying to combine the results of xp_cmdshell with a select query.
I have tried union & read about creating a temp table, but as my result will be having only 1 column. To be more precise i need a smaller query to combine the results of xp_cmdshell with select query as am trying to use it in union based sql injection
For example:
Select name from employee
union
exec xp_cmdshell 'whoami'
I know this wont work but somewhat similar would be great :)
You should know what this stored procedure returns and what outputs. Then when you know the table schema this procedure returns you may use the following syntax:
DECLARE #procedureOutput nvarchar(max)
SET #procedureOutput=Exec xp_cmdshell 'dir'
SELECT name FROM employee union #procedureOutput
If you need convert two different data types, CAST and CONVERT commands are for you.
Create a temp table and do insert into #temp EXEC.. or use OPENROESET. Refer this
http://beyondrelational.com/modules/2/blogs/70/posts/10812/select-columns-from-exec-procedurename-is-this-possible.aspx