SSIS 2005 - Create tables in db using queries in file with ext .sql
-
Wednesday, February 17, 2010 7:05 PM
Hello
I have sql quries (create table) in files (.sql) one for each table and i need to go through each and create tables in the database.
how can i accomplish this?
All Replies
-
Wednesday, February 17, 2010 7:16 PM
Use FoeEachFile to read the file data in script task and store this DDL script in a SSIS variable.Use this variable in Execute SQL Task.
Let us TRY this | Don’t forget to mark the post(s) that answered your question
http://quest4gen.blogspot.com/- Proposed As Answer by Eric WisdahlModerator Wednesday, February 17, 2010 7:53 PM
-
Wednesday, February 17, 2010 8:11 PM
can you share a sample using ForEachFile and code in script task
for example i have below in .sql file
CREATE TABLE IN_AIG(
MESSAGE_ID VARCHAR2(255) DEFAULT '',
PARENT_ID VARCHAR2(255) DEFAULT '',
AIG_1_ID INTEGER,
AIG_2_ACTION_CODE VARCHAR2(255),
AIG_3_RESOURCE_ID VARCHAR2(255),
AIG_3_RESOURCE_TX VARCHAR2(255),
AIG_3_RESOURCE_COD_SYS VARCHAR2(255),
AIG_4_RESOURCE_TYPE_ID VARCHAR2(255),
AIG_4_RESOURCE_TYPE_TX VARCHAR2(255),
AIG_4_RESOURCE_TYPE_COD_SYS VARCHAR2(255),
AIG_5_RESOURCE_GROUP_ID VARCHAR2(255),
AIG_5_RESOURCE_GROUP_TX VARCHAR2(255),
AIG_5_RESOURCE_GROUP_COD_SYS VARCHAR2(255),
AIG_6_RESOURCE_QTY DOUBLE PRECISION,
AIG_7_RES_UNITS_ID VARCHAR2(255),
AIG_7_RES_UNITS_TX VARCHAR2(255),
AIG_7_RES_UNITS_COD_SYS VARCHAR2(255),
AIG_8_START_DT DATE,
AIG_11_DURATION DOUBLE PRECISION,
AIG_12_DURATION_UNITS_ID VARCHAR2(255),
AIG_12_DURATION_UNITS_TX VARCHAR2(255),
AIG_12_DURATION_UNITS_COD_SYS VARCHAR2(255),
AIG_13_SUBST_CODE VARCHAR2(255),
AIG_14_FILLER_STATUS_ID VARCHAR2(255),
AIG_14_FILLER_STATUS_TX VARCHAR2(255),
AIG_14_FILLER_STATUS_COD_SYS VARCHAR2(255),
PRIMARY KEY (MESSAGE_ID,PARENT_ID,AIG_1_ID)); -
Wednesday, February 24, 2010 9:41 AM
Hello
I have sql quries (create table) in files (.sql) one for each table and i need to go through each and create tables in the database.
how can i accomplish this?
Hi Harris001,
Create a batch file that execute .sql files using sqlcmd command. This batch file can have input parameter as database name, server name and step from to start the execution.
find the sample code posted below.
@ECHO OFF
SET STEPNO=1
REM SET SERVER=%1
REM SET DB=%2
REM SET STEP=%3
:PRE1
SET /P SERVER=[Enter Database Server Name ] :
IF %SERVER%"dummy" == "dummy" (
echo No server name passed.;
GOTO :PRE1
)
:PRE2
SET /P DB=[Enter Database Name ] :
IF %DB%"dummy" == "dummy" (
echo No database name passed.;
GOTO :PRE2
)
SET /P STEPNO=[Enter the start step no ] :
IF %STEPNO%"dummy" == "dummy" (
echo No arguments passed. starting from STEP 1;
SET STEPNO = 1
) ELSE (
echo Argument is : %STEPNO%
)
PAUSE
GOTO STEP%STEPNO%
:STEP1
echo Now running STEP :%STEPNO%,'SCHEMA_SCRIPT_0.sql'
sqlcmd -S%SERVER% -d%DB% -E -i"SCHEMA_SCRIPT_0.sql" -o"error_output.txt" -b
IF NOT %errorlevel%==0 GOTO :ERROR
SET /A STEPNO = %STEPNO%+1
ECHO "All SQL Sripts have run successfully"
PAUSE
EXIT
:ERROR
echo "ERROR ENCOUNTERED AT STEP :"%STEPNO%
PAUSE
Hope this will solve your problem.
Thanks,
Mayur -
Wednesday, February 24, 2010 10:01 AMModerator
Use FoeEachFile to read the file data in script task and store this DDL script in a SSIS variable.
Use this variable in Execute SQL Task.
Let us TRY this | Don’t forget to mark the post(s) that answered your question
http://quest4gen.blogspot.com/
Or, even better, loop over the files using the For Each Loop's File Enumerator, set a File ConnectionManager's ConnectionString property to the enumerated value and set the Execute SQL Task's property SQLSourceType="File Connection". Thus the SQL will be read directly out of the file.
This will mean you don't have to use the Script Task to do the nasty reading of the file and storing it in a variable.
http://sqlblog.com/blogs/jamie_thomson/ | @jamiet | About me- Proposed As Answer by Jamie ThomsonMVP, Moderator Wednesday, February 24, 2010 10:01 AM
- Marked As Answer by Zongqing Li Thursday, February 25, 2010 7:16 AM
-
Thursday, February 25, 2010 6:45 AMModerator
I've got a blog post (with a link to a video) that shows how you can use the Execute SQL Task to execute .sql scripts:Hello
I have sql quries (create table) in files (.sql) one for each table and i need to go through each and create tables in the database.
how can i accomplish this?
.DoNotDisplay { display: none; }Hope that helps.<textarea id="ctl00___ctl00___ctl00_ctl00_bcr_ctl00___Entry___InlineTagEditor_Tags" style="width: 100%; height: 100%;"></textarea><input id="ctl00___ctl00___ctl00_ctl00_bcr_ctl00___Entry___InlineTagEditor_AllTags" type="hidden" value="ADO.Net+Data+Services&ADO.Net+Data+Services&Analysis+Services&Analysis+Services&astoria&astoria&Azure&Azure&Books+Online&Books+Online&codeplex&codeplex&collation&collation&Composable+DML&Composable+DML&Connect&Connect&CozyRoc&CozyRoc&csv&csv&data+modelling&data+modelling&data+warehousing&data+warehousing&Datadude&Datadude&date+representation&date+representation&datetime&datetime&dimension+cubes&dimension+cubes&excel&excel&Execute+SQL+Task&Execute+SQL+Task&expressions&expressions&For+Each+Loop&For+Each+Loop&Fuzzy&Fuzzy&GData&GData&ISO+8601&ISO+8601&Kimball&Kimball&linked+data&linked+data&Master+Data+Services&Master+Data+Services&MDS&MDS&merge&merge&Microsoft&Microsoft&msbuild&msbuild&Northwind&Northwind&nugget&nugget&OData&OData&Open+Data+Protocol&Open+Data+Protocol&pdc&pdc&precedence+constraints&precedence+constraints&Project+Dallas&Project+Dallas&Project+Houston&Project+Houston&RDF&RDF&Regex&Regex&reporting+services&reporting+services&rowcount&rowcount&Semantic+Engine&Semantic+Engine&Silverlight&Silverlight&slowly-changing-dimensions&slowly-changing-dimensions&Sort+transform&Sort+transform&sql&sql&SQL+Azure&SQL+Azure&sql+server&sql+server&sql+server+2008&sql+server+2008&sql+server+analysis+services&sql+server+analysis+services&sql+server+integration+services&sql+server+integration+services&SQL+Server+Reporting+Services&SQL+Server+Reporting+Services&SQLBits&SQLBits&SSAS&SSAS&ssis&ssis&SSISPackageStatsCollector&SSISPackageStatsCollector&SSRS&SSRS&StreamInsight&StreamInsight&surrogate+keys&surrogate+keys&t-sql&t-sql&T-SQL+Tuesday&T-SQL+Tuesday&T-SQL+Tuesday+001&T-SQL+Tuesday+001&tweetpoll&tweetpoll&twitter&twitter&twittercache&twittercache&User+Group&User+Group&video&video&Visio&Visio&VSTS+Database+Edition&VSTS+Database+Edition&WCF+Data+Services&WCF+Data+Services&xquery&xquery" />
http://sqlblog.com/blogs/jamie_thomson/ | @jamiet | About me- Proposed As Answer by Jamie ThomsonMVP, Moderator Sunday, February 28, 2010 9:23 PM

