473,385 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SQL Stored Procedure Insert List of variables

55
Hi,

I am running a stored procedure where I pass in a large list of variables e.g.

ALTER PROCEDURE dbo.Insert(@Variable1 varchar,
@Variable2 varchar,.....etc.

What I would like to know, is there a possible way to loop around all the variables, I think there is 16 of them, and insert them into a table with a new row for each variable in the list??

Many thanks in advance.
Feb 2 '09 #1
6 6316
ck9663
2,878 Expert 2GB
What you could do is to concatenate all the variables into a single variable separated by a comma (or a bar).

Then use this technique to convert it into a result set. Then you can do a single INSERT command.

Happy coding!

-- CK
Feb 2 '09 #2
dmj07
55
I changed my way of doing this and now have a varchar variable with the three values that I want to insert into the table all separated by a comma e.g.

DECLARE @Row1 varchar(10)

SET @Row1 = (SELECT Col1 + ',' + Col2 + ',' + Col3 FROM Table)

IF NOT(@Row1 = NULL)

BEGIN
INSERT INTO Table2
(ID, Col1, Col2, Col3)
VALUES (@ID, *********sticking point*********

END

As you can see from the code I want to split the @Row1 variable to separate them into three different inserts.

Many thanks.
Feb 3 '09 #3
ck9663
2,878 Expert 2GB
Are you planning to do something like this:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO Table2
  2.                      (ID, Col1)
  3.          VALUES (@ID, @FirstColumnInRow1)
  4.  
  5. INSERT INTO Table2
  6.                       (ID, Col2)
  7.           VALUES (@ID, @SecondColumnInRow1)
  8.  
  9. INSERT INTO Table2
  10.                       (ID, Col3)
  11.           VALUES (@ID, @ThirdColumnInRow1)
  12.  
That would mean you'll have rows with at least one empty CO1, COL2 and COL3.

Is that what you're trying to do?


-- CK
Feb 3 '09 #4
dmj07
55
Yes thats pretty much it
Feb 4 '09 #5
ck9663
2,878 Expert 2GB
The link I gave you will help you convert your string into a some sort of table that you can use to do a INSERT INTO...SELECT

-- CK
Feb 4 '09 #6
dmj07
55
Thanks for your help I got it working with the link you provided
Feb 5 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Luis | last post by:
I'm using a SQL Server 2000 stored procedure similar to the one below to upload data to a database. This data is collected from the user on a number of asp pages and stored in session variables...
1
by: RSH | last post by:
Im trying to create a stored procedure of the following code. I want to set it so that I have to send the first 4 variables (@DB, @BackUpFile,@TestDB,@RestoreFile). I am having trouble when i try...
1
by: ILCSP | last post by:
Hello, I'm trying to accomplish 3 things with one stored procedure. I'm trying to search for a record in table X, use the outcome of that search to insert another record in table Y and then exec...
2
by: karups | last post by:
Hi, I've got listbox in my .aspx page where the users can make multiple selection. So, Users can select any number of items in listbox, I have to take value from items and pass it to stored...
4
by: scparker | last post by:
Hello, We have a stored procedure that does a basic insert of values. I am then able to retrieve the ID number created for this new record. We are currently using ASP.NET 2.0 and use N-Tier...
1
by: jobs | last post by:
I have a growning list of stored procedures that accept a single string as a parameter. The list and parameters are maintained in a table with two columns. Some of the Stored procedures take...
9
by: Frawls | last post by:
Hi I Am am having problems with a stored Procedure that i wrote. Basically whats happening is that the Stored procedure Runs fine when i EXECUTE it in SQL Query analyzer. But when i debug...
6
by: yin_n_yang74 | last post by:
I am trying to create a report in Crystal Reports (v 8.5). I have a stored procedure to pull data from two databases and parameters. There are multiple one-to-many relationships and the stored...
3
by: stockton | last post by:
I have written the following Stored Procedure in an attempt to update two tables in the same database reliably but unfortunately it is not too successful. I ocassionaly end up with only the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.