34,590
社区成员
发帖
与我相关
我的任务
分享
sp_MSforeachdb
'if object_id("tAscPrmUser","u") is not null
print "?"'
declare @dbname nvarchar(100)
exec sp_MSforeachdb 'if object_id("tAscPrmUser","u") is not null set @dbname = @dbname + "?"'
print @dbname
if object_id('tempdb.dbo.#','U') is not null drop table #
go
create table tempdb.dbo.#(dbname varchar(200))
go
sp_MSforeachdb 'use ? if object_id("tAscPrmUser","u") is not null
insert into tempdb.dbo.# select "?"
'
declare @dbname nvarchar(2000)
select @dbname=isnull(@dbname+',','')+dbname from tempdb.dbo.#
select @dbname
create table tempdb.dbo.#(dbname varchar(200))
go
sp_MSforeachdb 'use ? if object_id("tAscPrmUser","u") is not null
insert into tempdb.dbo.# select "?"
'
declare @dbname nvarchar(2000)
select @dbname=isnull(@dbname+',','')+dbname from tempdb.dbo.#
select @dbname
drop table tempdb.dbo.#
use test
go
CREATE TABLE t(a VARCHAR(200))
go
exec sp_msforeachdb 'use ? if object_id(''表'') is not null insert test.dbo.t select ''?'''
go
SELECT * FROM t
输出参数
declare @num int, @sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?
declare @num int, @sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num
sp_MSforeachdb 'use ? if object_id("T_1","u") is not null print "?"'
sp_MSforeachdb
'use ?
if object_id("tAscPrmUser","u") is not null
print "?"'