Solved

TSQL q

Posted on 2015-02-11
5
97 Views
Last Modified: 2015-02-14
Hello,

I have a comma seperated string and a function called SPLIT that parses it and returns a table with one value per row.

1,2,3,4,5,6
gives
1
2
3
4
5
6

I now want to insert these values into a table that has three columns.

MyId, CodeA, CodeB

where MyId is 10, and CodeA is 20.

So i will get

10, 20, 1
10, 20, 2
10, 20, 3
10, 20, 4
10, 20, 5
10, 20, 6

It must be very simple but i cannot get my head round the insert statement.

How do i do it?
0
Comment
Question by:soozh
5 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
Can you post here a sample of the original data (where that 10 and 20 comes from)?
0
 
LVL 1

Expert Comment

by:uday kiran reddy
Comment Utility
If you are using a separate PL like java,c# or scripting language like VBS,etc. which will send the insert statement to database.
Then you can write multiple loops to handle the same and send separate insert statement to database for each value in the first table generated by SPLIT function.
0
 

Author Comment

by:soozh
Comment Utility
10 and 20 are constants.....

so insert (MyId, CodeA, CodeB) values (10, 20, <and here the parsed strings from the function Slit>)

I could do it (pseudo code)

insert (MyId, CodeA, CodeB) values (10, 20, string[1] );
insert (MyId, CodeA, CodeB) values (10, 20, string[2] );
insert (MyId, CodeA, CodeB) values (10, 20, string[3] );
insert (MyId, CodeA, CodeB) values (10, 20, string[4] );

etc

but i guess uts possible with one line of tsql and a clever way of using the rows returned by split.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
Without knowing anything about your function, perhaps something like this?

a lot depends on how you normally call that function and what it returns (assuming it returns a table)


select 10 as const1, 20 as const2 , r.*
from ( select dbo.split('1.2.3.4.5.6')  )  AS r
0
 

Author Closing Comment

by:soozh
Comment Utility
perfect!
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now