SQL Server trigger, insert result of multiple where clause into a variable

20 pts.
Tags:
SELECT statement
SQL
SQL variables
Triggers
Hi All, I have a select statement that stores the result in a variable,this works fine if there is only one condition and field in the clause.How do I go about it if there is more than one? For example (Working one)
SELECT @Variable = (SELECT [Field] FROM [Table] WHERE [FIELD] = Value)
But I am looking for something like this
IF EXIST(SELECT * FROM [Table] WHERE [FIELD1] = Value1 AND [Field2] = Value2)
BEGIN
INSERT
@Variable
VALUE
[FIELD3]
END
Thanks Alot

Answer Wiki

Thanks. We'll let you know when a new response is added.

Try something like this.

INSERT INTO Table
(Field1)
SELECT Field3
FROM INSERTED
WHERE NOT EXISTS (SELECT * FROM Table WHERE Table.Field1 = INSERTED.Field3)

Hi
Thanks … came right

What I did was to create a static table and used the insert into statement to filter on my physical table and then just used the static table throughout my trigger

Thanks again

Discuss This Question: 1  Reply

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    Could you please explain what exactly are you trying to accomplish ? In your second example it does not seem that you are trying to assign the query results to variables, but just verifying if some record satisfying those conditions exists. Are you getting errors when putting more than one condition in the where clause ?
    69,175 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following