MSSQL Stored Procedure – Return more than one column
Asked By: NicT
Originally Asked On: 2014-01-05 18:23:39
Asked Via: stackoverflow
(If you’re here from Google, I answered my own question with a fix)
I have some trouble running a stored procedure from a WAMS (Windows Azure Mobile Service).
It only returns the last row in the relevant table, when I would like to get all the relevant tables for the query.
The stored procedure looks sort of like the following:
DECLARE @id bigint SET @id = 0 IF @var IS NOT NULL AND @var!= 0 BEGIN SELECT @id = id FROM intersection WHERE var = @var IF @id IS NOT NULL BEGIN SELECT row1, row2, row3 FROM relevant_table WHERE id = @id END END
As you can see, I’m trying to return some rows based on a variable I’ve defined using the WA SQL Manager Interface.
The procedure executes as it is now, however it only returns the last row I’ve ever defined, and not all of the rows that’s relevant to the ID I get from the ‘intersection’ table.
My WAMS API script is pretty basic. Executes the Stored Procedure based on a query string value, saves it to a response variable, which it will return.
My intersection table is structured like the following:
intesection_id(int) var(int) id(int)-foreign key = fkey_0 1 10009 1 2 10009 2
And my relevant_table is like this:
id(int)-foreign key = fkey_0 information information2 created 1 "some info" "other info" default = getdate() 2 "other info" "even more" --||--
The “var” column is yet another foreign key, but that’s to a table which is irrelevant to my problem.
He received 4 answers
eventually accepting:
Bill Stidham’s answer to
MSSQL Stored Procedure – Return more than one column
You don’t need all of those IF statements. They kind of clutter up the query IMO. Your query could be re-written as follows:
;WITH parms AS (SELECT [Var] = @var ) SELECT rt.row1 , rt.row2 , rt.row3 FROM parms p JOIN intersection isec ON isec.[var] = p.[Var] JOIN relevant_table rt ON rt.id = isec.id WHERE ISNULL(p.[Var], 0) <> 0
This query will return an empty set if
@var
is NULL or0
. If you just don’t want it to select anything at all if@var
is NULL or if@var
is not null but@id
is null then you could still use the nested IF structure in your question, but if that is not your intention then both checks are totally necessary.Regardless of what you do, the
IF @var IS NOT NULL AND @var!= 0
statement can be summarized withIF ISNULL(@var, 0) <> 0
which is represented asWHERE ISNULL(p.[Var], 0) > 0
in my query.You’re also calling your columns “rows” in the last
SELECT
. I left them as “row1, row2, …”.
The answer with the highest score with 1 points was:
YuvalHerziger’s answer to
MSSQL Stored Procedure – Return more than one column
Just a shot in the dark, perhaps you should change
IF @var IS NOT NULL AND @var!= 0
Into
IF @var IS NOT NULL AND @var <> 0
If the selected answer did not help you out, the other answers might!
All Answers For: MSSQL Stored Procedure – Return more than one column
YuvalHerziger’s answer to
MSSQL Stored Procedure – Return more than one column
Just a shot in the dark, perhaps you should change
IF @var IS NOT NULL AND @var!= 0
Into
IF @var IS NOT NULL AND @var <> 0
M.Ali’s answer to
MSSQL Stored Procedure – Return more than one column
Test Data
DECLARE @intersection TABLE (intesection_id INT, [Var] INT, ID INT) INSERT INTO @intersection VALUES (1,10009,1),(2,10009,2),(3,10010,2),(4,10010,2) DECLARE @relevant_table TABLE (id int,information VARCHAR(20),information2 VARCHAR(20) ,created DATETIME) INSERT INTO @relevant_table VALUES (1,'some info','other info',getdate()), (2,'other info','even more',getdate())
Query
DECLARE @id int; DECLARE @var int; SET @var = 10009 --<-- @var value from your proc IF (@var IS NOT NULL AND @var <> 0) BEGIN SELECT TOP 1 @id = ID FROM @intersection WHERE [Var] = @var END IF (@id IS NOT NULL AND @id <> 0) BEGIN SELECT * FROM @intersection I INNER JOIN @relevant_table R ON I.ID = R.id WHERE R.id = @id END
Result Set
╔════════════════╦═══════╦════╦════╦═════════════╦══════════════╦═════════════════════════╗ ║ intesection_id ║ Var ║ ID ║ id ║ information ║ information2 ║ created ║ ╠════════════════╬═══════╬════╬════╬═════════════╬══════════════╬═════════════════════════╣ ║ 1 ║ 10009 ║ 1 ║ 1 ║ some info ║ other info ║ 2014-01-05 19:42:52.450 ║ ╚════════════════╩═══════╩════╩════╩═════════════╩══════════════╩═════════════════════════╝
Bill Stidham’s answer to
MSSQL Stored Procedure – Return more than one column
You don’t need all of those IF statements. They kind of clutter up the query IMO. Your query could be re-written as follows:
;WITH parms AS (SELECT [Var] = @var ) SELECT rt.row1 , rt.row2 , rt.row3 FROM parms p JOIN intersection isec ON isec.[var] = p.[Var] JOIN relevant_table rt ON rt.id = isec.id WHERE ISNULL(p.[Var], 0) <> 0
This query will return an empty set if
@var
is NULL or0
. If you just don’t want it to select anything at all if@var
is NULL or if@var
is not null but@id
is null then you could still use the nested IF structure in your question, but if that is not your intention then both checks are totally necessary.Regardless of what you do, the
IF @var IS NOT NULL AND @var!= 0
statement can be summarized withIF ISNULL(@var, 0) <> 0
which is represented asWHERE ISNULL(p.[Var], 0) > 0
in my query.You’re also calling your columns “rows” in the last
SELECT
. I left them as “row1, row2, …”.
NicT’s answer to
MSSQL Stored Procedure – Return more than one column
I fixed my issue by going the safe way, and adding a view on top. I can afford it.
T-SQL in View:
SELECT intersection.intersection_id, intersection.var, relevant_table.id, relevant_table.information, relevant_table.information2 FROM dbo.intersection INNER JOIN dbo.relevant_table ON relevant_table.id = intersection.id
T-SQL in sp:
SELECT id, information, information2 FROM View WHERE var = @var
This let me do pretty much what I wanted.
NODE.js in WAMS Api Editor is as easy as this:
var mssql = request.service.mssql; var sql_query = "EXEC sp_getvar ?"; mssql.query(sql_query, [var], { success: function(res) { response.send(statusCodes.OK, res) },
Not having to deal with business logic in my API was my main goal, this basically just makes it easier to add API calls on top of, instead of having to change every individual api file if I wanted to make a change.
Of course, you should really check out the original question.
The post MSSQL Stored Procedure – Return more than one column [ANSWERED] appeared first on Tech ABC to XYZ.