Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to Load a Script File into Table?

Author  Topic 

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-03-14 : 16:17:47
Do anyone know of an easy way to load the contents of a SQL script file into a NVARCHAR(MAX) column in a table?

It would be very handy to be able to do this so I can have SQL scripts loaded in a table and then execute them from a job.

Declare @MyScript nvarchar(max)

select @MyScript = MyScript from MyScriptTable where ScriptName = ‘MySQLScript’

execute ( @MyScript )


CODO ERGO SUM

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-03-14 : 16:48:24
Try this:

INSERT INTO SomeTable(Column1)
EXEC master.dbo.xp_cmdshell 'type c:\SomeScriptFile.txt'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-03-14 : 17:12:04
quote:
Originally posted by tkizer

Try this:

INSERT INTO SomeTable(Column1)
EXEC master.dbo.xp_cmdshell 'type c:\SomeScriptFile.txt'

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/



The xp_cmdshell output returns multiple rows, so that isn't really what I need.

I want to be able to put the script into a single column of a single row of a table.






CODO ERGO SUM
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-03-14 : 17:37:13
I'm pretty sure that you are going to have to do like I showed and put it into a staging table and then move your data to your actual table in the way that you want it. You'll probably need to concatenate each row together to get it into one row, one column.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-14 : 17:55:33
Also see
http://weblogs.sqlteam.com/peterl/archive/2007/09/26/Insert-binary-data-like-images-into-SQL-Server-without-front-end.aspx



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-03-14 : 18:05:14
I found a solution and just posted a script to do this in the Script Library.

Execute Script from a File:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=99084

CODO ERGO SUM
Go to Top of Page
   

- Advertisement -