Solved

TSQL q

Posted on 2015-02-11
5
99 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 49

Expert Comment

by:Vitor Montalvão
ID: 40602827
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
ID: 40602838
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
ID: 40602859
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
ID: 40602890
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
ID: 40610009
perfect!
0

Featured Post

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL- GROUP BY 4 21
Need SSIS project 2 23
how to extract last 6 digits from a varchar, sql 4 19
Database-Scoped Permissions 2 11
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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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…
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.

713 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