SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Dynamically convert rows to cloumns


Dynamically convert rows to cloumns

Author
Message
sqltester1
sqltester1
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 5
Hi All,
Need a help on the following.
I have a the following code it returning the result : db1,db2 2017-12-27 14:09:15.000 \\share1\test1_F.bak,\\share1\test1_A.bak.bak

But i want to insert each parameters to a table given by the user.
Ex:
insert into tbl @manual_dbname paramter1,getdate(), @manual_path1
insert into tbl @manual_dbname paramter2,getdate(), @manual_path2
like:

db1 2017-12-27 14:09:15.000 \\share1\test1_F.bak
db2 2017-12-27 14:09:15.000 \\share1\test1_A.bak.bak


code:
declare @manual_dbname varchar (1000)
declare @manual_backupdate varchar (1000)
declare @manual_path varchar (1000)

set @manual_dbname ='db1,db2'
set @manual_backupdate ='2017-12-12 14:09:15.000'
set @manual_path ='\\share1\test1_F.bak,\\share1\test1_A.bak.bak'

select @manual_dbname,@manual_backupdate,@manual_path


SELECT SUBSTRING(@manual_dbname,0,CHARINDEX (',',@manual_dbname,0))

Thanks for the help.
John Mitchell-245523
John Mitchell-245523
SSC Guru
SSC Guru (132K reputation)SSC Guru (132K reputation)SSC Guru (132K reputation)SSC Guru (132K reputation)SSC Guru (132K reputation)SSC Guru (132K reputation)SSC Guru (132K reputation)SSC Guru (132K reputation)

Group: General Forum Members
Points: 132108 Visits: 19225
You'll need to use a splitter function to break your strings down into their individual elements, something like this (not tested because I don't actually have the splitter function on my server):

WITH DBs AS (
SELECT Item AS DBName, ItemNumber
FROM dbo.DelimitedSplit8k(@manual_dbname,',')
)
, Paths AS (
SELECT Item AS BackupPath, ItemNumber
FROM dbo.DelimitedSplit8k(@manual_path,',')
)
SELECT
d.DBName
, @manual_backupdate AS BackupDate
, p.BackupPath
FROM DBs d
JOIN Paths p ON d.ItemNumber = p.ItemNumber


John
sqltester1
sqltester1
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
Points: 27 Visits: 5
Thanks a lot John. It's perfectly working.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search