Solved

TSQL q

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

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 eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

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.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

696 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