Temporary SQL datafield

2020-05-02 00:46发布

问题:

Currently I am returning a boolean value as "1" or "0" or value of a column let say column A in one of my stored procedure and it works perfectly, however in one of application I am trying to get value like this

string temp = reader.TryGetValue("ColumnA", out licenseStatus) ? temp : string.Empty;

Stored procedure is doing something like this,

if @datafield = 1
    return 1
else if @datafield = 2 AND @datafield2 = 3
    return 0
else
    return (SELECT ColumnA.table2 FROM table1 INNER JOIN table2 on table1.id and table2.id Where @datafield3 = 0)

Now I want to return result as value of ColumnA always instead of sending "0" or "1" as I am not allowed to make changes to code in application.

Is it possible ? I can't make any changes to ColumnA value at all either.

EDIT

I already mentioned in my question that stored procedure is doing what its suppose to do but not working with my code, as I am returning 1 or 0 at some point and my code isn't getting it as columnA so its setting it up empty, I want to return 0 or 1 as value of ColumnA but at same time I don't want to change value of columnA in table2..

:)

回答1:

if @datafield = 1
set ColumnA.table2=1
else if @datafield = 2 AND @datafield2 = 3
set ColumnA.table2= 0
else
return (SELECT ColumnA.table2 FROM table1 INNER JOIN table2 on table1.id and table2.id Where @datafield3 = 0)

If it is only a select query then it will not change your table data rather it'll return what you want



回答2:

You can use CASE

select 
    case @datafield 
    when 1 then 1 
    when 2 then 0 
    when 3 then 0 
    else ColumnA 
    end as ColumnA
from ....