Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


IF EXISTS Logic


IF EXISTS Logic

Author
Message
Minnu
Minnu
Old Hand
Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)Old Hand (305 reputation)

Group: General Forum Members
Points: 305 Visits: 950
Hi Team,

I am having a temp Table "@Temp_Table" and a physical Table "St_Value"

@Temp_Table Contains
Case1:
Column_Name | Value
------------------------------
Col1 123
Col2 NULL


"St_value" Contains
Column_Name | Value
-------------------------------
Col1 123
Col2 582
Col3


My Query is :
if @temp_table contains Col1 and its value then

select "col1 : 123"


for record col2 there is no value (NULL) then
only the column lable should be include.

select "Col1 :123,
Col2 :"

then for remaining records it should check in "St_value" table



Final string should be

select "Col1 :123,
Col2 :
Col3 :456"

Exact Requirement :If temp table contains label "column_name" and its "value" then only Column_Name : Value should be included
else only column_name should be included, for remaining records it should check in physical table.

am using below logic, but its not working.

Please help..


SELECT @query1 =
IF EXISTS (SELECT col1 from @Temp_Table)THEN
'Col1: ' +rtrim(Col1) + ' (Value ' + rtrim(convert(varchar, Value,107)) + ')' +CHAR(10)

ELSE
'Col1: ' ' Value : '

ELSE
CASE WHEN Col1 IS NULL or Col1 = '' THEN ''

ELSE CASE WHEN Value IS null then
'Col1: ' + rtrim(Col1) +CHAR(10)
ELSE 'Col1: ' +rtrim(Col1) + ' (Expires ' + rtrim(convert(varchar, Value,107)) + ')' +CHAR(10)
END
END+

from St_Value


Please Help...?
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3042 Visits: 5478
Why are you trying to use dynamic sql. It's look like you need full outer join:

SELECT ISNULL(t.Column_Name, v.Column_Name) AS Column_Name
,CASE WHEN t.Column_Name IS NULL THEN v.Value
ELSE t.Value
END AS Value
FROM @Temp_Table t
FULL OUTER JOIN St_Value v
ON v.Column_Name = t.Column_Name



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
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