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

IF EXISTS Logic Expand / Collapse
Author
Message
Posted Monday, December 3, 2012 1:01 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, August 28, 2014 1:09 AM
Points: 227, Visits: 715
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...?
Post #1391794
Posted Monday, December 3, 2012 2:36 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 7:27 AM
Points: 2,836, Visits: 5,067
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1391817
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse