Showing posts with label statments. Show all posts
Showing posts with label statments. Show all posts

Thursday, March 29, 2012

Executing Script Files From Transact-SQL

Hi,

I have my create statments for tables, procedures, views, etc in
individual Transact-SQL script files (.sql).

I wnat to write another script file that executes these scripts in the
correct order to create the database.

What is the syntax for executing script files from Transact-SQL?

Thanks, PhilPhil (hp_howell@.hotmail.com) writes:
> I have my create statments for tables, procedures, views, etc in
> individual Transact-SQL script files (.sql).
> I wnat to write another script file that executes these scripts in the
> correct order to create the database.
> What is the syntax for executing script files from Transact-SQL?

There isn't one really. Once the batch has been sent to SQL Server,
the script is executing on the server and not on the machine where you
have the scripts.

You can, though, use xp_cmdshell to fork out and run a script through a
command-line tool like OSQL. Beware then that you are running from a second
connection.

Another alternative is to run the scripts with OSQL from the client machine,
and use ~r to include files. Note that ~r is a command to OSQL, and is not
understood by Query Analyzer or SQL Server.

My personal preference for install scripts is to run them in some client
language (Perl in my case). This does not have to be advanced. Basically
just something which reads the files, passes it to SQL Server through some
API call or through OSQL, and then maybe checks for errors.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.aspsql

Friday, March 9, 2012

Execute SQL Statment

Hello,
I need to execute "SQL Statments" that belong to a script
but i think that im not doing it in the right way.
I only can print the output, but i dont know how to
execute it.
I've th following code:
if (select count(1) from master..sysdatabases) > 0
declare dbname_cur cursor for select [name] from
master..sysdatabases
declare @.dbname as varchar(500)
declare @.sqlstring as varchar(500
)
open dbname_cur
fetch next from dbname_cur into @.dbname
WHILE @.@.FETCH_STATUS = 0
begin
set @.dbname = 'select [name],[filename] from ' + @.dbname
+ '..sysfiles'
print @.dbname
fetch next from dbname_cur into @.dbname
end
close dbname_cur
deallocate dbname_cur
This code give me the following output:
select [name],[filename] from example..sysfiles
select [name],[filename] from master..sysfiles
select [name],[filename] from model..sysfiles
select [name],[filename] from msdb..sysfiles
select [name],[filename] from Northwind..sysfiles
select [name],[filename] from pubs..sysfiles
select [name],[filename] from tempdb..sysfiles
Now i need to execute this output.
Best regardsPlace the follwoing code after the "print @.dbname" line in your cursor loop:
exec (@.dbname)
--Buddy
"CC&JM" <anonymous@.discussions.microsoft.com> wrote in message
news:0e4b01c42613$2eeb8e80$a601280a@.phx.gbl...
> Hello,
> I need to execute "SQL Statments" that belong to a script
> but i think that im not doing it in the right way.
> I only can print the output, but i dont know how to
> execute it.
> I've th following code:
> if (select count(1) from master..sysdatabases) > 0
> declare dbname_cur cursor for select [name] from
> master..sysdatabases
> declare @.dbname as varchar(500)
> declare @.sqlstring as varchar(500
> )
> open dbname_cur
> fetch next from dbname_cur into @.dbname
> WHILE @.@.FETCH_STATUS = 0
> begin
> set @.dbname = 'select [name],[filename] from ' + @.dbname
> + '..sysfiles'
> print @.dbname
> fetch next from dbname_cur into @.dbname
> end
> close dbname_cur
> deallocate dbname_cur
> This code give me the following output:
> select [name],[filename] from example..sysfiles
> select [name],[filename] from master..sysfiles
> select [name],[filename] from model..sysfiles
> select [name],[filename] from msdb..sysfiles
> select [name],[filename] from Northwind..sysfiles
> select [name],[filename] from pubs..sysfiles
> select [name],[filename] from tempdb..sysfiles
> Now i need to execute this output.
> Best regards
>|||Use:
EXEC (@.dbname)
instead of or right after print @.dbname.
Jacco Schalkwijk
SQL Server MVP
"CC&JM" <anonymous@.discussions.microsoft.com> wrote in message
news:0e4b01c42613$2eeb8e80$a601280a@.phx.gbl...
> Hello,
> I need to execute "SQL Statments" that belong to a script
> but i think that im not doing it in the right way.
> I only can print the output, but i dont know how to
> execute it.
> I've th following code:
> if (select count(1) from master..sysdatabases) > 0
> declare dbname_cur cursor for select [name] from
> master..sysdatabases
> declare @.dbname as varchar(500)
> declare @.sqlstring as varchar(500
> )
> open dbname_cur
> fetch next from dbname_cur into @.dbname
> WHILE @.@.FETCH_STATUS = 0
> begin
> set @.dbname = 'select [name],[filename] from ' + @.dbname
> + '..sysfiles'
> print @.dbname
> fetch next from dbname_cur into @.dbname
> end
> close dbname_cur
> deallocate dbname_cur
> This code give me the following output:
> select [name],[filename] from example..sysfiles
> select [name],[filename] from master..sysfiles
> select [name],[filename] from model..sysfiles
> select [name],[filename] from msdb..sysfiles
> select [name],[filename] from Northwind..sysfiles
> select [name],[filename] from pubs..sysfiles
> select [name],[filename] from tempdb..sysfiles
> Now i need to execute this output.
> Best regards
>

Wednesday, March 7, 2012

Execute SQL Statment

Hello,
I need to execute "SQL Statments" that belong to a script
but i think that im not doing it in the right way.
I only can print the output, but i dont know how to
execute it.
I've th following code:
if (select count(1) from master..sysdatabases) > 0
declare dbname_cur cursor for select [name] from
master..sysdatabases
declare @.dbname as varchar(500)
declare @.sqlstring as varchar(500
)
open dbname_cur
fetch next from dbname_cur into @.dbname
WHILE @.@.FETCH_STATUS = 0
begin
set @.dbname = 'select [name],[filename] from ' + @.dbname
+ '..sysfiles'
print @.dbname
fetch next from dbname_cur into @.dbname
end
close dbname_cur
deallocate dbname_cur
This code give me the following output:
select [name],[filename] from example..sysfiles
select [name],[filename] from master..sysfiles
select [name],[filename] from model..sysfiles
select [name],[filename] from msdb..sysfiles
select [name],[filename] from Northwind..sysfiles
select [name],[filename] from pubs..sysfiles
select [name],[filename] from tempdb..sysfiles
Now i need to execute this output.
Best regards
Place the follwoing code after the "print @.dbname" line in your cursor loop:
exec (@.dbname)
--Buddy
"CC&JM" <anonymous@.discussions.microsoft.com> wrote in message
news:0e4b01c42613$2eeb8e80$a601280a@.phx.gbl...
> Hello,
> I need to execute "SQL Statments" that belong to a script
> but i think that im not doing it in the right way.
> I only can print the output, but i dont know how to
> execute it.
> I've th following code:
> if (select count(1) from master..sysdatabases) > 0
> declare dbname_cur cursor for select [name] from
> master..sysdatabases
> declare @.dbname as varchar(500)
> declare @.sqlstring as varchar(500
> )
> open dbname_cur
> fetch next from dbname_cur into @.dbname
> WHILE @.@.FETCH_STATUS = 0
> begin
> set @.dbname = 'select [name],[filename] from ' + @.dbname
> + '..sysfiles'
> print @.dbname
> fetch next from dbname_cur into @.dbname
> end
> close dbname_cur
> deallocate dbname_cur
> This code give me the following output:
> select [name],[filename] from example..sysfiles
> select [name],[filename] from master..sysfiles
> select [name],[filename] from model..sysfiles
> select [name],[filename] from msdb..sysfiles
> select [name],[filename] from Northwind..sysfiles
> select [name],[filename] from pubs..sysfiles
> select [name],[filename] from tempdb..sysfiles
> Now i need to execute this output.
> Best regards
>
|||Use:
EXEC (@.dbname)
instead of or right after print @.dbname.
Jacco Schalkwijk
SQL Server MVP
"CC&JM" <anonymous@.discussions.microsoft.com> wrote in message
news:0e4b01c42613$2eeb8e80$a601280a@.phx.gbl...
> Hello,
> I need to execute "SQL Statments" that belong to a script
> but i think that im not doing it in the right way.
> I only can print the output, but i dont know how to
> execute it.
> I've th following code:
> if (select count(1) from master..sysdatabases) > 0
> declare dbname_cur cursor for select [name] from
> master..sysdatabases
> declare @.dbname as varchar(500)
> declare @.sqlstring as varchar(500
> )
> open dbname_cur
> fetch next from dbname_cur into @.dbname
> WHILE @.@.FETCH_STATUS = 0
> begin
> set @.dbname = 'select [name],[filename] from ' + @.dbname
> + '..sysfiles'
> print @.dbname
> fetch next from dbname_cur into @.dbname
> end
> close dbname_cur
> deallocate dbname_cur
> This code give me the following output:
> select [name],[filename] from example..sysfiles
> select [name],[filename] from master..sysfiles
> select [name],[filename] from model..sysfiles
> select [name],[filename] from msdb..sysfiles
> select [name],[filename] from Northwind..sysfiles
> select [name],[filename] from pubs..sysfiles
> select [name],[filename] from tempdb..sysfiles
> Now i need to execute this output.
> Best regards
>

Execute SQL Statment

Hello,
I need to execute "SQL Statments" that belong to a script
but i think that im not doing it in the right way.
I only can print the output, but i dont know how to
execute it.
I've th following code:
if (select count(1) from master..sysdatabases) > 0
declare dbname_cur cursor for select [name] from
master..sysdatabases
declare @.dbname as varchar(500)
declare @.sqlstring as varchar(500
)
open dbname_cur
fetch next from dbname_cur into @.dbname
WHILE @.@.FETCH_STATUS = 0
begin
set @.dbname = 'select [name],[filename] from ' + @.dbname
+ '..sysfiles'
print @.dbname
fetch next from dbname_cur into @.dbname
end
close dbname_cur
deallocate dbname_cur
This code give me the following output:
select [name],[filename] from example..sysfiles
select [name],[filename] from master..sysfiles
select [name],[filename] from model..sysfiles
select [name],[filename] from msdb..sysfiles
select [name],[filename] from Northwind..sysfiles
select [name],[filename] from pubs..sysfiles
select [name],[filename] from tempdb..sysfiles
Now i need to execute this output.
Best regardsPlace the follwoing code after the "print @.dbname" line in your cursor loop:
exec (@.dbname)
--Buddy
"CC&JM" <anonymous@.discussions.microsoft.com> wrote in message
news:0e4b01c42613$2eeb8e80$a601280a@.phx.gbl...
> Hello,
> I need to execute "SQL Statments" that belong to a script
> but i think that im not doing it in the right way.
> I only can print the output, but i dont know how to
> execute it.
> I've th following code:
> if (select count(1) from master..sysdatabases) > 0
> declare dbname_cur cursor for select [name] from
> master..sysdatabases
> declare @.dbname as varchar(500)
> declare @.sqlstring as varchar(500
> )
> open dbname_cur
> fetch next from dbname_cur into @.dbname
> WHILE @.@.FETCH_STATUS = 0
> begin
> set @.dbname = 'select [name],[filename] from ' + @.dbname
> + '..sysfiles'
> print @.dbname
> fetch next from dbname_cur into @.dbname
> end
> close dbname_cur
> deallocate dbname_cur
> This code give me the following output:
> select [name],[filename] from example..sysfiles
> select [name],[filename] from master..sysfiles
> select [name],[filename] from model..sysfiles
> select [name],[filename] from msdb..sysfiles
> select [name],[filename] from Northwind..sysfiles
> select [name],[filename] from pubs..sysfiles
> select [name],[filename] from tempdb..sysfiles
> Now i need to execute this output.
> Best regards
>|||Use:
EXEC (@.dbname)
instead of or right after print @.dbname.
--
Jacco Schalkwijk
SQL Server MVP
"CC&JM" <anonymous@.discussions.microsoft.com> wrote in message
news:0e4b01c42613$2eeb8e80$a601280a@.phx.gbl...
> Hello,
> I need to execute "SQL Statments" that belong to a script
> but i think that im not doing it in the right way.
> I only can print the output, but i dont know how to
> execute it.
> I've th following code:
> if (select count(1) from master..sysdatabases) > 0
> declare dbname_cur cursor for select [name] from
> master..sysdatabases
> declare @.dbname as varchar(500)
> declare @.sqlstring as varchar(500
> )
> open dbname_cur
> fetch next from dbname_cur into @.dbname
> WHILE @.@.FETCH_STATUS = 0
> begin
> set @.dbname = 'select [name],[filename] from ' + @.dbname
> + '..sysfiles'
> print @.dbname
> fetch next from dbname_cur into @.dbname
> end
> close dbname_cur
> deallocate dbname_cur
> This code give me the following output:
> select [name],[filename] from example..sysfiles
> select [name],[filename] from master..sysfiles
> select [name],[filename] from model..sysfiles
> select [name],[filename] from msdb..sysfiles
> select [name],[filename] from Northwind..sysfiles
> select [name],[filename] from pubs..sysfiles
> select [name],[filename] from tempdb..sysfiles
> Now i need to execute this output.
> Best regards
>|||After print @.dbname, add
exec (@.dbname)
Regards,
Paul Ibison|||Thanks everybody,
Best Regards
>--Original Message--
>Hello,
>I need to execute "SQL Statments" that belong to a script
>but i think that im not doing it in the right way.
>I only can print the output, but i dont know how to
>execute it.
>I've th following code:
>if (select count(1) from master..sysdatabases) > 0
> declare dbname_cur cursor for select [name] from
>master..sysdatabases
> declare @.dbname as varchar(500)
> declare @.sqlstring as varchar(500
>)
> open dbname_cur
> fetch next from dbname_cur into @.dbname
> WHILE @.@.FETCH_STATUS = 0
> begin
> set @.dbname = 'select [name],[filename] from ' +
@.dbname
>+ '..sysfiles'
> print @.dbname
> fetch next from dbname_cur into @.dbname
> end
>close dbname_cur
>deallocate dbname_cur
>This code give me the following output:
>select [name],[filename] from example..sysfiles
>select [name],[filename] from master..sysfiles
>select [name],[filename] from model..sysfiles
>select [name],[filename] from msdb..sysfiles
>select [name],[filename] from Northwind..sysfiles
>select [name],[filename] from pubs..sysfiles
>select [name],[filename] from tempdb..sysfiles
>Now i need to execute this output.
>Best regards
>.
>