传递当前的dbName并执行过程

时间:2018-01-08 12:59:33

标签: sql-server stored-procedures

看到这个......

SELECT * 
INTO #WL_Klijenti
FROM  OPENROWSET ('SQLOLEDB','Server=(local);TRUSTED_CONNECTION=YES;','SET 
FMTONLY OFF; SET NOCOUNT ON; EXEC 
WBANKA_KBBL2.dbo.sp_kbbl_WachLista_Priprema ''2017.09.30'', ''2017.09.30'', 
0')
    AS tbl
  

部分:EXEC WBANKA_KBBL2应该替换为当前使用的db,这样用户就不必手动指定它。

我知道一个带有输出参数的过程,它将返回当前数据库并将其存储在变量中,这是我的尝试:

SP看起来像这样:

CREATE procedure dbo.getCurrentDB
(

@dbName varchar(30) OUTPUT

)
AS

BEGIN

set @dbName = (select db_name())
--select @dbName

END
go

如何将当前使用的数据库传递给过程调用,而不是手动指定?

编辑:

当我像这样运行时,我收到一个SELECT语句,它为变量赋值不能与数据检索操作结合

declare @dbName varchar(30)
exec getCurrentDB @dbName output

DECLARE @ORS varchar(MAX);


SELECT @ORS = 'Server=(local);TRUSTED_CONNECTION=YES;','SET 
FMTONLY OFF; SET NOCOUNT ON; exec
select ' + QUOTENAME([name] + '.dbo.sp_kbbl_WachLista_Priprema 
''2017.09.30'', ''2017.09.30'', 
0')
FROM sys.databases
WHERE [name] = @dbName

SELECT * 
INTO #WL_Klijenti
FROM  OPENROWSET ('SQLOLEDB',@ORS)
 AS tbl;

0 个答案:

没有答案