Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

Returning multiple records when only one is specified Expand / Collapse
Author
Message
Posted Wednesday, February 5, 2014 5:19 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, June 27, 2014 10:04 PM
Points: 60, Visits: 71
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
Post #1538121
Posted Wednesday, February 5, 2014 5:26 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 8:23 AM
Points: 370, Visits: 650
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
----------------------------------------
Post #1538123
Posted Wednesday, February 5, 2014 5:34 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, June 27, 2014 10:04 PM
Points: 60, Visits: 71
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'
Post #1538126
Posted Wednesday, February 5, 2014 5:37 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, June 27, 2014 10:04 PM
Points: 60, Visits: 71
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.
Post #1538127
Posted Wednesday, February 5, 2014 5:48 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 8:23 AM
Points: 370, Visits: 650
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
----------------------------------------
Post #1538138
Posted Wednesday, February 5, 2014 6:03 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, June 27, 2014 10:04 PM
Points: 60, Visits: 71
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?

Post #1538144
Posted Wednesday, February 5, 2014 6:13 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:51 AM
Points: 7,127, Visits: 13,503
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
Post #1538148
Posted Wednesday, February 5, 2014 6:35 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, June 27, 2014 10:04 PM
Points: 60, Visits: 71
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
Post #1538154
Posted Wednesday, February 5, 2014 6:40 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 8:23 AM
Points: 370, Visits: 650
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
----------------------------------------
Post #1538158
Posted Wednesday, February 5, 2014 6:45 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:51 AM
Points: 7,127, Visits: 13,503
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
Post #1538161
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse