Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Possible to Insert Multiple Values Into One Variable

Status
Not open for further replies.

seabaz2000

IS-IT--Management
Feb 24, 2006
79
0
0
IE
Hi
I was just wondering is it possible to Insert Data from many fields into a single variable and then insert the data from that variable into a single field on another table.
Eg
Table 1 Contains
Coding
Testing
Integration
Select the three of them, Place them into a single variable called @variable
and write the data to
Table 2 as
Coding, Integration, Testing
 
Easiest way is create a function that concatenates the values, like below.

But unless you are data warehousing... usually a better way is to keep the data normalized with relational tables. I use the function below for reporting and warehousing, but not not for transaction processing.

Code:
Create FUNCTION dbo.udf_GetCategoryNames(@contactid INT, 
                    @contactname VARCHAR(30))
RETURNS VARCHAR(2000)
AS
BEGIN
DECLARE @v_out  VARCHAR(2000)
SET @v_out = ''

SELECT @v_out = @v_out + ',' + ca.CategoryName 
FROM tblContacts c INNER JOIN
tblContactCategories cc ON c.ContactID = cc.fContactID INNER JOIN
tblCategories ca ON cc.fCategoryID = ca.CategoryID
WHERE c.ContactId=@contactid 
and c.ContactName = @contactname
   
RETURN(SUBSTRING(@v_out, 3, LEN(@v_out)))
END
 
Code:
Select @variable= Coding + ',' + Testing + ',' + @Integration from Table1 where ...
Insert into Table2 Values (@variable)

?
 
Oh, crap. I read that wrong. My example is for many records into one field. Chris' example is for many fields into one field. Ignore me; pay attention to Chris. Principle is the same, just depends if you want to go up and downs or side to side.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top