Solved

TSQL q

Posted on 2015-02-11
5
101 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
[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
5 Comments
 
LVL 50

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

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

739 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