Discussion Locked

Update Qurey For MS SQL

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I need to make conditional update qurey. It has to compare the one of field in the record which will be updated. Is there anyway to make condition?

2 replies

by lehttn

You can use PL/SQL to make a script like this:
declare @field1 numeric;
select @field1 = Field1 from Table1 where ...;
if @field1 == @condition1
update Table1 set ... where ...;
--
(@condition1 is the value you want to compare Field1 with)Good luck.

by bucky kaufman (mcsd)

You can't use PL/SQL on SQL Server (MS SQL). You have to use T-SQL (Transact SQL).

To solve this problem, the user should use a "CASE" expression.

You COULD use a version of the PL/SQL method prior to this post. However, it calls for the user to create a paramaterized query, and then to call that query with the parameters set. Issuing two seperate SQL statements as one is not something you can do with T-SQL. So you'd have to create the query as a Stored Procedure at design time, instead of composing it as a non-compiled SQL statement ad runtime.

This discussion is closed to new replies.