SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Returning multiple records when only one is specified


Returning multiple records when only one is specified

Author
Message
crazy_new
crazy_new
Old Hand
Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)

Group: General Forum Members
Points: 387 Visits: 375
If I set the variable to a value in the column, and then set the var = to the column it also work, but i dont want to use it like that, because i will only use the nameid to search and not the address as well
BenWard
BenWard
SSChasing Mays
SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)

Group: General Forum Members
Points: 654 Visits: 827
ok lets see if this is what you're after...


IF (EXISTS(
SELECT TOP 1 1
FROM PAT_NAMES_ADDRESS
WHERE NAME_ID = @NAMEID
AND ADR2 IS NULL
AND ADR3 IS NULL
))
BEGIN

SELECT ADR1,
POSTAL_CODE,
(STATE_NAME) AS COUNTRY
FROM PAT_NAMES_ADDRESS AS P
INNER JOIN STATE_NAME AS S ON P.STATE_ID = S.STATE_ID
WHERE P.NAME_ID = @NAMEID
AND P.CURRENT_ONE = '1'
AND P.LANGUAGE_ID = '3'

END





Ben

^ Thats me!


----------------------------------------
01010111011010000110000101110100 01100001 0110001101101111011011010111000001101100011001010111010001100101 01110100011010010110110101100101 011101110110000101110011011101000110010101110010
----------------------------------------
crazy_new
crazy_new
Old Hand
Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)

Group: General Forum Members
Points: 387 Visits: 375
Let me explain a bit better what I need to do and then post my whole code.

I need to write a script, where i can insert a name id, and the it will return the adr1, adr2,adr3 postal code and country.
If any of those are null, it should not select that column i.e. if adr3 is null, it should only return the other columns. If adr2 and adr3 is null, it should select all the other columns except those to.

Here is all me code:


DECLARE @NAMEID int, @ADR2 VARCHAR(100), @ADR3 VARCHAR(100)
SET @NAMEID = '14388'



IF @ADR2 IS NULL AND @ADR3 IS NULL

SELECT ADR1, POSTAL_CODE, (STATE_NAME) AS COUNTRY
FROM PAT_NAMES_ADDRESS P, STATE_NAME S
WHERE P.NAME_ID = @nameid
AND P.STATE_ID = S.STATE_ID
AND P.CURRENT_ONE = '1'
AND S.LANGUAGE_ID = '3'


ELSE IF @ADR3 IS NULL AND @ADR2 IS NOT NULL

SELECT ADR1,ADR2, POSTAL_CODE, (STATE_NAME) AS COUNTRY
FROM PAT_NAMES_ADDRESS P, STATE_NAME S
WHERE P.NAME_ID = @nameid
AND P.STATE_ID = S.STATE_ID
AND P.CURRENT_ONE = '1'
AND S.LANGUAGE_ID = '3'

IF @ADR2 IS NULL AND @ADR3 IS NOT NULL

SELECT ADR1,ADR3, POSTAL_CODE, (STATE_NAME) AS COUNTRY
FROM PAT_NAMES_ADDRESS P, STATE_NAME S
WHERE P.NAME_ID = @nameid
AND P.STATE_ID = S.STATE_ID
AND P.CURRENT_ONE = '1'
AND S.LANGUAGE_ID = '3'

ELSE IF @ADR2 IS NOT NULL AND @ADR3 IS NOT NULL

SELECT ADR1, ADR2, ADR3, POSTAL_CODE, (STATE_NAME) AS COUNTRY
FROM PAT_NAMES_ADDRESS P, STATE_NAME S
WHERE P.NAME_ID = @nameid
AND P.STATE_ID = S.STATE_ID
AND P.CURRENT_ONE = '1'
AND S.LANGUAGE_ID = '3'
crazy_new
crazy_new
Old Hand
Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)

Group: General Forum Members
Points: 387 Visits: 375
If I exclude the if statement, the SELECT works perfectly.
I just need to know if and how I can change the IF statements, so the conditions the the IF will work.
BenWard
BenWard
SSChasing Mays
SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)

Group: General Forum Members
Points: 654 Visits: 827
ah I see.

This should be easier than you think.

DECLARE @NAMEID int, @ADR2 VARCHAR(100), @ADR3 VARCHAR(100)
SET @NAMEID = '14388'

--just add this line
SELECT @ADR2 = ADR2, @ADR3 = ADR3 FROM PAT_NAMES_ADDRESS WHERE NAME_ID = @NAMEID

IF @ADR2 IS NULL AND @ADR3 IS NULL

SELECT ADR1, POSTAL_CODE, (STATE_NAME) AS COUNTRY
FROM PAT_NAMES_ADDRESS P, STATE_NAME S
WHERE P.NAME_ID = @nameid
AND P.STATE_ID = S.STATE_ID
AND P.CURRENT_ONE = '1'
AND S.LANGUAGE_ID = '3'


ELSE IF @ADR3 IS NULL AND @ADR2 IS NOT NULL

SELECT ADR1,ADR2, POSTAL_CODE, (STATE_NAME) AS COUNTRY
FROM PAT_NAMES_ADDRESS P, STATE_NAME S
WHERE P.NAME_ID = @nameid
AND P.STATE_ID = S.STATE_ID
AND P.CURRENT_ONE = '1'
AND S.LANGUAGE_ID = '3'

IF @ADR2 IS NULL AND @ADR3 IS NOT NULL

SELECT ADR1,ADR3, POSTAL_CODE, (STATE_NAME) AS COUNTRY
FROM PAT_NAMES_ADDRESS P, STATE_NAME S
WHERE P.NAME_ID = @nameid
AND P.STATE_ID = S.STATE_ID
AND P.CURRENT_ONE = '1'
AND S.LANGUAGE_ID = '3'

ELSE IF @ADR2 IS NOT NULL AND @ADR3 IS NOT NULL

SELECT ADR1, ADR2, ADR3, POSTAL_CODE, (STATE_NAME) AS COUNTRY
FROM PAT_NAMES_ADDRESS P, STATE_NAME S
WHERE P.NAME_ID = @nameid
AND P.STATE_ID = S.STATE_ID
AND P.CURRENT_ONE = '1'
AND S.LANGUAGE_ID = '3'



Ben

^ Thats me!


----------------------------------------
01010111011010000110000101110100 01100001 0110001101101111011011010111000001101100011001010111010001100101 01110100011010010110110101100101 011101110110000101110011011101000110010101110010
----------------------------------------
crazy_new
crazy_new
Old Hand
Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)

Group: General Forum Members
Points: 387 Visits: 375
Ah i didnt know I could select like that.

Thanks Ben!!!
:-)

Just want to confirm one more thing with you, I can't put all this code into a view, and just query the view hey?

Because of all the options?
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16010 Visits: 19524
What's going to consume the result set from the view? How will it handle an unknown number of columns with some unknown names? Can't you concatenate the variable address elements into one address column called e.g. [address]?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
crazy_new
crazy_new
Old Hand
Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)

Group: General Forum Members
Points: 387 Visits: 375
My boss wants this script as a view, and I told him you cant put this in a view, just wanted to make sure im right.

Thanks for your help man, I really appreciate it
BenWard
BenWard
SSChasing Mays
SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)SSChasing Mays (654 reputation)

Group: General Forum Members
Points: 654 Visits: 827
Probably the best option if you need to preserve that variable number of output columns is to bung the lot into a stored procedure and use @NAMEID as a parameter.

http://msdn.microsoft.com/en-us/library/ms345415(v=sql.120).aspx


you'll then be able to execute it with something like

EXEC sprocFindAddress @NAMEID = 12345

Ben

^ Thats me!


----------------------------------------
01010111011010000110000101110100 01100001 0110001101101111011011010111000001101100011001010111010001100101 01110100011010010110110101100101 011101110110000101110011011101000110010101110010
----------------------------------------
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16010 Visits: 19524
crazy_new (2/5/2014)
My boss wants this script as a view, and I told him you cant put this in a view, just wanted to make sure im right.

Thanks for your help man, I really appreciate it


You need to know the answer to this: what's going to consume a result set with an unknown number of columns?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search