September 3, 2007 at 12:46 pm
I have the following syntax within an SP:
Declare @CompleteHP as varchar(5000)
SELECT
t2.PE1
, t2.PE2
, t2.ProgIntro
FROM PClinical t2
WHERE t2.PartyID=36008
SET @CompleteHP= t2.ProgIntro
I get an error that t2 is not recognized as an alias. What am I missing in my syntax?
THanks,
Sam
September 3, 2007 at 2:28 pm
You can't use a select to return data and set a variable at the same time. You must do this in 2 steps.
Also you must consider that the select can return more than 1 line and that you'll never know which line will be saved in the variable (it's always the last one, but unless you use order by, then there's no way to garantee that).
September 3, 2007 at 4:20 pm
Thanks,
Next question is if the return data is null, how do I set the declared variable to '' (empty string)?
Here's what I have so far that's not working:
SELECT @FHComm=p.TextBody
FROM PartyText p
WHERE DataPointID=167 And p.PartyID=@PartyID
CASE WHEN @FHComm Is Null THEN ''
ELSE @FHComm END
I have a whole list of variables that I am concantenating to make a complete note. But nothing shows up if even a single piece of data is missing (null).
September 3, 2007 at 4:25 pm
SELECT @MyVar = ISNULL(ColName, '') FROM dbo...
September 4, 2007 at 12:01 pm
thanks very much. I've got it licked.
Sam
September 4, 2007 at 12:18 pm
I will even suggest one step better :
SET @MyVar = ISNULL((SELECT ColName FROM Table WHERE Condtition = Returns1RowOnly), '')
That way if hte select returns more than 1 row, the statement will bomb. That's a safer way to do things.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply