Solved

Set Multiple values on a Variable

Posted on 2009-03-31
10
328 Views
Last Modified: 2012-05-06
I have the following MSSQL line in my trigger
declare @varID varchar(1000)
set @varID = (SELECT [varID] FROM deleted)

the problem is that the query is returning more than one value and is causing an erro.
So let's say value1,value2 and value3

I want the varID to be equal to "value1,value2,value3"

How can i do that ?


0
Comment
Question by:cscg1976
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24029784
Hello cscg1976,

How are you using @varID?

Regards,

Patrick
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24029896
You can create a comma separated value using the steps given in the link:

http://blog.namwarrizvi.com/?p=10

Pass the value to @varID
Its done.
0
 
LVL 28

Expert Comment

by:TextReport
ID: 24029983
You can use a cursor
Cheers, Andrew
DECLARE @varID varchar(1000)
DECLARE @Text AS VarChar(100)
 
DECLARE Object_Cursor CURSOR FOR
   SELECT [varID] FROM deleted
 
OPEN Object_Cursor
 
FETCH NEXT FROM Object_Cursor INTO @Text
	  
WHILE @@FETCH_STATUS = 0
  BEGIN
    IF @varID IS NULL
       SET @varID = @Text
    ELSE
       SET @varID = @VarID + ', ' + @Text
	
    FETCH NEXT FROM Object_Cursor INTO @Text
  END
 
CLOSE Object_Cursor
 
SELECT @varID AS rv
 
DEALLOCATE Object_Cursor

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24030029
usually, you will use a JOIN or EXISTS or IN clause later...

please do NOT use a cursor in your trigger
please try to avoid "programming" solutions if you can do set-based solutions.
0
 
LVL 28

Expert Comment

by:TextReport
ID: 24030256
angelIII "please do NOT use a cursor in your trigger" absolutely sorry I went back to the question and forgot about the trigger.
cscg1976, please disregard my posting
Cheers, Andrew
0
 

Author Comment

by:cscg1976
ID: 24030294
I'm using it to set an sqlQuery to
set @sqlQuery = 'DELETE FROM Table1 WHERE varID IN (''' + @varID + ''')'
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 24030331
simple:
DELETE FROM Table1 WHERE varID IN ( SELECT [varID] FROM deleted )
 
or
 
DELETE t1 
 FROM Table1 t1
  JOIN deleted d
    on d.varid = t.varID 

Open in new window

0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 250 total points
ID: 24030335
cscg1976,

With respect, you are going about this the wrong way :)

Instead, just run the delete directly:

DELETE Table1
FROM deleted d INNER JOIN
      Table1 t ON d.varID = t.varID

Regards,

Patrick
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 24030345
I need faster fingers, apparently :)
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Select Query help 1 38
ODBC settings not showng in FileMaker External Data Sources 6 24
access to sql migration 5 26
T-SQL: problem comparing datetime 4 53
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question