在SQL的存儲過程,觸發(fā)中不能使用USE。怎么辦呢,當(dāng)然可以直接用[databasename].[user].[tablename]這樣來訪問,但是某些語句必須在當(dāng)前數(shù)據(jù)庫下執(zhí)行,比如sp_adduser等。
原文:
這里提供一個解決方案: (考試大提示:這個程序運行有問題)
使用Exec(ute)語句來改變當(dāng)前數(shù)據(jù)庫,不過要注意的是,數(shù)據(jù)庫環(huán)境的更改只在Execute語句結(jié)束前有效,所以你必須把后面的語句和Execute放到一起。Example:
create proc test
as
exec(’use pubs’) exec sp_adduser ’test’
go
exec test
根據(jù)下面的語句執(zhí)行沒有問題:
使用Exec(ute)語句來改變當(dāng)前數(shù)據(jù)庫。
如下:
CREATE PROCEDURE cs
AS
BEGIN
exec(’use sjdbmis; select plucode from txtplu’)
END
GO
又如:
CREATE PROCEDURE cs
AS
BEGIN
exec(’use sjdbmis; exec sp_adduser ’’test’’’)
END
GO
原文:
這里提供一個解決方案: (考試大提示:這個程序運行有問題)
使用Exec(ute)語句來改變當(dāng)前數(shù)據(jù)庫,不過要注意的是,數(shù)據(jù)庫環(huán)境的更改只在Execute語句結(jié)束前有效,所以你必須把后面的語句和Execute放到一起。Example:
create proc test
as
exec(’use pubs’) exec sp_adduser ’test’
go
exec test
根據(jù)下面的語句執(zhí)行沒有問題:
使用Exec(ute)語句來改變當(dāng)前數(shù)據(jù)庫。
如下:
CREATE PROCEDURE cs
AS
BEGIN
exec(’use sjdbmis; select plucode from txtplu’)
END
GO
又如:
CREATE PROCEDURE cs
AS
BEGIN
exec(’use sjdbmis; exec sp_adduser ’’test’’’)
END
GO