在T-SQL中使用当前数据库名称有Using语句

发布于 2024-09-02 12:03:37 字数 2014 浏览 1 评论 0原文

我有应用程序运行 T-SQL 语句来更新多个数据库 问题是我正在使用以下 t-sql

USE [msdb]
GO
DECLARE @jobId BINARY(16)
EXEC  msdb.dbo.sp_add_job @job_name=N'test2', 
        @enabled=1, 
        @start_step_id=1, 
        @notify_level_eventlog=0, 
        @notify_level_email=2, 
        @notify_level_netsend=2, 
        @notify_level_page=2, 
        @delete_level=0, 
        @description=N'', 
        @category_name=N'[Uncategorized (Local)]', 
        @owner_login_name=N'sa', 
        @notify_email_operator_name=N'', 
        @notify_netsend_operator_name=N'', 
        @notify_page_operator_name=N'', @job_id = @jobId OUTPUT
select @jobId
GO
EXEC msdb.dbo.sp_add_jobserver @job_name=N'test2', @server_name = N'AMR-PC\SQL2008'
GO
USE [msdb]
GO
EXEC msdb.dbo.sp_add_jobstep @job_name=N'test2', @step_name=N'test', 
        @step_id=1, 
        @cmdexec_success_code=0, 
        @on_success_action=1, 
        @on_fail_action=2, 
        @retry_attempts=0, 
        @retry_interval=0, 
        @os_run_priority=0, @subsystem=N'TSQL', 
        @command=N'EXEC sp_MSforeachdb ''
    EXEC sp_MSforeachtable @command1=''''DBCC DBREINDEX (''''''''*'''''''')'''', 
        @replacechar=''''*''''''', 
        @database_name=N'Client5281', 
        @output_file_name=N'C:\Documents and Settings\Amr\Desktop\Scheduel Reports\report', 
        @flags=2
GO

USE [msdb]
GO
DECLARE @schedule_id int
EXEC msdb.dbo.sp_add_jobschedule @job_name=N'test2', @name=N'test', 
        @enabled=1, 
        @freq_type=8, 
        @freq_interval=1, 
        @freq_subday_type=1, 
        @freq_subday_interval=0, 
        @freq_relative_interval=0, 
        @freq_recurrence_factor=1, 
        @active_start_date=20100517, 
        @active_end_date=99991231, 
        @active_start_time=0, 
        @active_end_time=235959, @schedule_id = @schedule_id OUTPUT
select @schedule_id
GO

并且我在任何块之前使用 (USE [msdb]) 并且我想获取数据库名称来替换此 @database_name=N'**Client5281**' , 使用当前数据库名称而不是我正在使用的 ([msdb])。

我希望我能很好地解释我想要什么。

I have application runs T-SQL statements to update more than one database
the problem is i'm using the following t-sql

USE [msdb]
GO
DECLARE @jobId BINARY(16)
EXEC  msdb.dbo.sp_add_job @job_name=N'test2', 
        @enabled=1, 
        @start_step_id=1, 
        @notify_level_eventlog=0, 
        @notify_level_email=2, 
        @notify_level_netsend=2, 
        @notify_level_page=2, 
        @delete_level=0, 
        @description=N'', 
        @category_name=N'[Uncategorized (Local)]', 
        @owner_login_name=N'sa', 
        @notify_email_operator_name=N'', 
        @notify_netsend_operator_name=N'', 
        @notify_page_operator_name=N'', @job_id = @jobId OUTPUT
select @jobId
GO
EXEC msdb.dbo.sp_add_jobserver @job_name=N'test2', @server_name = N'AMR-PC\SQL2008'
GO
USE [msdb]
GO
EXEC msdb.dbo.sp_add_jobstep @job_name=N'test2', @step_name=N'test', 
        @step_id=1, 
        @cmdexec_success_code=0, 
        @on_success_action=1, 
        @on_fail_action=2, 
        @retry_attempts=0, 
        @retry_interval=0, 
        @os_run_priority=0, @subsystem=N'TSQL', 
        @command=N'EXEC sp_MSforeachdb ''
    EXEC sp_MSforeachtable @command1=''''DBCC DBREINDEX (''''''''*'''''''')'''', 
        @replacechar=''''*''''''', 
        @database_name=N'Client5281', 
        @output_file_name=N'C:\Documents and Settings\Amr\Desktop\Scheduel Reports\report', 
        @flags=2
GO

USE [msdb]
GO
DECLARE @schedule_id int
EXEC msdb.dbo.sp_add_jobschedule @job_name=N'test2', @name=N'test', 
        @enabled=1, 
        @freq_type=8, 
        @freq_interval=1, 
        @freq_subday_type=1, 
        @freq_subday_interval=0, 
        @freq_relative_interval=0, 
        @freq_recurrence_factor=1, 
        @active_start_date=20100517, 
        @active_end_date=99991231, 
        @active_start_time=0, 
        @active_end_time=235959, @schedule_id = @schedule_id OUTPUT
select @schedule_id
GO

and i'm using (USE [msdb]) before any block and i want to get database name to replace this @database_name=N'**Client5281**', with the current database name instead of ([msdb]) that i'm using.

i hope that i explained what i want well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃扇骨 2024-09-09 12:03:37

删除所有use [msdb],它们是不需要的。由于脚本使用数据库名称和模式名称完全限定过程名称,因此无需在调用过程之前使用 USE。然后您可以简单地使用@database_name=DB_NAME ()

另一种解决方案是使用 SQLCMD 变量,正确配置后 SSMS 可以识别该变量:

:setvar dbname Client5281
...
USE [msdb]
GO
EXEC msdb.dbo.sp_add_jobstep @job_name=N'test2', @step_name=N'test', 
        ...
        @database_name=N'$(dbname)', 
        ...
GO

Remove all use [msdb], they are not needed. Since the script fully qualifies the procedure name with the db name and schema name, there is no need to USE before invoking the procedure. You can then simply use @database_name=DB_NAME ().

Another solution is to use SQLCMD variables, which SSMS recognizes when properly configured:

:setvar dbname Client5281
...
USE [msdb]
GO
EXEC msdb.dbo.sp_add_jobstep @job_name=N'test2', @step_name=N'test', 
        ...
        @database_name=N'$(dbname)', 
        ...
GO
谜泪 2024-09-09 12:03:37

实现这一点的唯一方法是动态 SQL。例如:

declare @sql nvarchar(max)
set @sql = N'use ' + @database_name + '
    go
    declare @schedule_id int
    ...'
exec (@sql)

为了响应您的评论,您可以保存当前数据库名称,例如:

declare @db varchar(256)
set @db = DB_NAME()

然后将:更改

@database_name=N'Client5281', 

为:

@db,

您还必须删除任何 go 语句,因为它们限制了变量范围。

The only way to do that is dynamic SQL. For example:

declare @sql nvarchar(max)
set @sql = N'use ' + @database_name + '
    go
    declare @schedule_id int
    ...'
exec (@sql)

In response to your comment, you can save the current database name like:

declare @db varchar(256)
set @db = DB_NAME()

And then change:

@database_name=N'Client5281', 

to:

@db,

You will also have to remove any go statements, as those limit the variable scope.

旧人 2024-09-09 12:03:37

因为我从vb.net应用程序执行此查询,所以我使用replace()方法将dbname替换为当前dbname。感谢所有试图帮助我的人。

beacause i execute this query from vb.net application i used replace() method to replace the dbname by current dbname. thanks for all tried to help me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文