Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to execute a procedure at certain time and how to use database in cursor

Posted on 2010-01-05
4
Medium Priority
?
478 Views
Last Modified: 2012-08-13
1.How can i activate/exec a procedure at a certain time on routine basis.
2. I want to use all thedatabases and print all the tables inside two databases.
But the following code is not working.
Declare @t_database varchar(300)
Declare @t_table varchar(300)
Declare curdatabase cursor for
select name from sys.databases where database_id > 4 order by name
OPEN curdatabase
FETCH curdatabase into @t_database
WHILE(@@fetch_status=0)
BEGIN
            print 'Database : = '+@t_database
            DECLARE @s NVARCHAR(MAX)
            SET @s = 'USE ' + @T_DATABASE
            EXECUTE sp_executesql @s

            Declare curtables cursor for
            select name from sys.tables order by name
            OPEN curtables
            FETCH curtables into @t_table
            WHILE(@@fetch_status=0)
                  BEGIN
                        Print 'Table = '+@t_table
                        FETCH curtables into @t_table
                  END
      CLOSE curtables
      DEALLOCATE curtables

FETCH curdatabase into @t_database
END
CLOSE curdatabase
DEALLOCATE curdatabase
0
Comment
Question by:searchsanjaysharma
4 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 200 total points
ID: 26184093
SELECT  TABLE_NAME FROM db1.INFORMATION_SCHEMA.TABLES
UNION
SELECT  TABLE_NAME FROM db2.INFORMATION_SCHEMA.TABLES
0
 
LVL 26

Assisted Solution

by:tigin44
tigin44 earned 200 total points
ID: 26184218
1. you can use the sql agent jobs to schedule a procedure..

2. the following code will list the table names... just correct the db names.

use db1
go
sp_MSforeachtable @command1="print '?'"
go
use db2
go
sp_MSforeachtable @command1="print '?'"
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 1600 total points
ID: 26184244
In order to run a procedure at a scheduled time, use SQL Server Agent (SQL Jobs).

for #2, you have to put your cursur inside dynamic SQL like one of the below.
Declare @t_database varchar(300)
Declare curdatabase cursor for
select name from sys.databases where database_id > 4 order by name
OPEN curdatabase
FETCH curdatabase into @t_database
WHILE(@@fetch_status=0)
BEGIN
            print 'Database : = '+@t_database
            DECLARE @s NVARCHAR(MAX)
            SET @s = 'USE ' + @T_DATABASE + '
Declare @t_table varchar(300)
            Declare curtables cursor for
            select name from sys.tables order by name
            OPEN curtables
            FETCH curtables into @t_table
            WHILE(@@fetch_status=0)
                  BEGIN
                        Print ''Table = ''+@t_table
                        FETCH curtables into @t_table
                  END
      CLOSE curtables
      DEALLOCATE curtables
'
            EXECUTE sp_executesql @s
FETCH curdatabase into @t_database
END
CLOSE curdatabase
DEALLOCATE curdatabase t




------



Declare @t_database varchar(300)
,@s nvarchar(max)

Declare curdatabase cursor for
select name from sys.databases where database_id > 4 order by name
OPEN curdatabase
FETCH curdatabase into @t_database
WHILE(@@fetch_status=0)
BEGIN
     set @s = 'use ' + quotename(@s) + '
     select table_name from ' + quotename(@s) + '.information_schema.tables order by table_name'

            EXECUTE sp_executesql @s
FETCH curdatabase into @t_database
END
CLOSE curdatabase
DEALLOCATE curdatabase t

Open in new window

0
 

Author Closing Comment

by:searchsanjaysharma
ID: 31673140
ok
0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

810 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