Discussion:
variable FIELD query
(too old to reply)
Bob
2012-06-07 20:07:31 UTC
Permalink
Hi folks. I previously posted this question but I made an error in my
example.

I need to update a column of a table based on the pri. key.

I tried using Case like this

Case @MIY
When 0 Then Update dbo.EMI Set fieldA=@insertString Where CID=@CID
When 1 Then Update dbo.EMI Set fieldB=@insertString Where CID=@CID
When 2 Then Update dbo.EMI Set fieldC=@insertString Where CID=@CID
When 3 Then Update dbo.EMI Set fieldD=@insertString Where CID=@CID
End

That didn't work so I tried

If @MIY=0
Begin
Update dbo.EMI Set fieldA=@insertString Where CID=@CID
End
If @MIY=1
Begin
Update dbo.EMI Set fieldB=@insertString Where CID=@CID
End

ect ect ect (I have 11 IF's)

That didn't work either, although I saw other examples that seemed to
work. What am I missing?

Anybody got any ideas?

Thanks, Bob
Erland Sommarskog
2012-06-07 21:26:59 UTC
Permalink
Post by Bob
Begin
End
Begin
End
ect ect ect (I have 11 IF's)
That didn't work either, although I saw other examples that seemed to
work. What am I missing?
What does "didn't work" mean? Did you get an error message? Unexpected
results? Did you server reach nirvana?
--
Erland Sommarskog, SQL Server MVP, ***@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
Gene Wirchenko
2012-06-07 21:58:39 UTC
Permalink
On Thu, 07 Jun 2012 23:26:59 +0200, Erland Sommarskog
Post by Erland Sommarskog
Post by Bob
Begin
End
Begin
End
ect ect ect (I have 11 IF's)
That didn't work either, although I saw other examples that seemed to
work. What am I missing?
What does "didn't work" mean? Did you get an error message? Unexpected
results? Did you server reach nirvana?
OP, please read this before asking questions here:
http://www.catb.org/~esr/faqs/smart-questions.html
How To Ask Questions The Smart Way

Erland is a very helpful sort, but he (and anyone else helping)
needs good-quality data to have a reasonable chance of actually
helping.

The link is very good. It will help you figure out how to
organise your questions for maximum effect. Sometimes, following what
it suggests, you may find that you have uncovered enough further
detail that you can solve your problem yourself. If not, there will
still be far more useful data for anyone who might try to answer your
question.

Sincerely,

Gene Wirchenko

Sincerely,

Gene Wirchenko
rpresser
2012-06-08 13:30:44 UTC
Permalink
On Thursday, June 7, 2012 4:07:31 PM UTC-4, Bob wrote:

Your case idea would never work. Case is an expression construct; you cannot embed whole statements in it.

Your IF structure should have worked from what I can see.

More information about what you are trying to accomplish and what your server did in response is required to provide any help.
Loading...