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


If Statement


If Statement

Author
Message
crazy_new
crazy_new
SSC-Addicted
SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)

Group: General Forum Members
Points: 413 Visits: 375
The following is an example of an IF statement im busy with. It has to check if ADR3 is null, if it is it should only select ADR1, ADR2 and postal code. If its not null it should also select ADR3. There are no syntax errors or anything, it just looks at the first IF, so it doesn't return ADR3, whether it is Null or not.



DECLARE @ADR3 VARCHAR(100), @ADDRESS VARCHAR(100)

SET @ADR3 = (SELECT ADR3
FROM PAT_NAMES_ADDRESS
WHERE ADR1 = @ADDRESS)

SET @ADDRESS = 'P O Box 2258'

IF @ADR3 IS NULL


SELECT ADR1, ADR2, POSTAL_CODE
FROM PAT_NAMES_ADDRESS
WHERE ADR1 = @ADDRESS


ELSE IF @ADR3 IS NOT NULL

SELECT ADR1, ADR2,ADR3 , POSTAL_CODE
FROM PAT_NAMES_ADDRESS
WHERE ADR1 = @ADDRESS


How can I fix this if it doesn't give me an error?
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14802 Visits: 15980
The first thing to try is to put a SELECT @ADR3 statement in just after you set the value of the variable. Then you can see whether the problem is that the variable is not being set correctly, or that your IF logic is not working as expected.

John
crazy_new
crazy_new
SSC-Addicted
SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)

Group: General Forum Members
Points: 413 Visits: 375
Hi John,

I di what you said: SET @ADR3 = (SELECT ADR3
FROM PAT_NAMES_ADDRESS
WHERE ADR1 = @ADDRESS)
SELECT @ADR3

The value it returns is null.
But it doesn't make sense because when I check it outside of an if statement:

SELECT ADR1, ADR2,adr3, POSTAL_CODE
FROM pat_names_address
WHERE ADR3 is not null
AND ADR1 = 'M & B Pumps Division'

It does return a value.

Why is that?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91099 Visits: 45284
Where are you setting the value of @ADDRESS?

In your code, you have:

DECLARE @ADR3 VARCHAR(100), @ADDRESS VARCHAR(100)

SET @ADR3 = (SELECT ADR3
FROM PAT_NAMES_ADDRESS
WHERE ADR1 = @ADDRESS)

So you're declaring @ADDRESS then using it before setting a value. By default it's null, so unless you left out the code where you give @ADDRESS a value, your set boils down to this:

SET @ADR3 = (SELECT ADR3
FROM PAT_NAMES_ADDRESS
WHERE ADR1 = NULL)

Which will never return rows.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


crazy_new
crazy_new
SSC-Addicted
SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)

Group: General Forum Members
Points: 413 Visits: 375
Just below that I set the value.


SET @ADR3 = (SELECT ADR3
FROM PAT_NAMES_ADDRESS
WHERE ADR1 = @ADDRESS)

SET @ADDRESS = 'M & B Pumps Division'
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14802 Visits: 15980
You need to do it before, not after.

John
crazy_new
crazy_new
SSC-Addicted
SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)SSC-Addicted (413 reputation)

Group: General Forum Members
Points: 413 Visits: 375
When I do it before then I get the following error.

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91099 Visits: 45284
crazy_new (1/28/2014)
Just below that I set the value.


SET @ADR3 = (SELECT ADR3
FROM PAT_NAMES_ADDRESS
WHERE ADR1 = @ADDRESS)

SET @ADDRESS = 'M & B Pumps Division'


So search for all rows matching a specific address, then after it's finished the search set the value to the address that should have been searched for?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14802 Visits: 15980
OK, so fix that. When you set the value of a scalar variable, you can only pass a single value to it. If the SELECT statement you use to set it returns more than one row, you'll get that error.

John
GilaMonster
GilaMonster
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91099 Visits: 45284
crazy_new (1/28/2014)
When I do it before then I get the following error.

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.


That means you have more than one row which matches that value. Should you have duplicate rows? If you do have multiple rows which match that address, which one do you want?

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


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