?
Solved

TSQL q

Posted on 2015-02-11
5
Medium Priority
?
103 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 51

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 49

Accepted Solution

by:
PortletPaul earned 2000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

752 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