使用变量确定要从中进行选择的表

发布于 2024-12-22 20:56:39 字数 329 浏览 1 评论 0原文

这是我得到的新错误:

IF object_id('Osvezi') IS NULL

EXEC sp_executesql N' 
CREATE PROCEDURE Osvezi
    -- Add the parameters for the stored procedure here
    @tablica nvarchar(200)
AS
BEGIN
   IF @tablica = 'Uporabniki' THEN SELECT * FROM Uporabniki;

   IF @tablica = 'Dokumenti' THEN SELECT * from Dokumenti;
END'
GO

this is new error I get:

IF object_id('Osvezi') IS NULL

EXEC sp_executesql N' 
CREATE PROCEDURE Osvezi
    -- Add the parameters for the stored procedure here
    @tablica nvarchar(200)
AS
BEGIN
   IF @tablica = 'Uporabniki' THEN SELECT * FROM Uporabniki;

   IF @tablica = 'Dokumenti' THEN SELECT * from Dokumenti;
END'
GO

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

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

发布评论

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

评论(2

小…红帽 2024-12-29 20:56:39

您可以这样做,但不推荐这样做,

CREATE PROCEDURE Osvezi
    -- Add the parameters for the stored procedure here
    @tablica nvarchar(200)
AS
BEGIN
    EXECUTE('SELECT * FROM' + @tablica);
END

这也取决于您使用的 DBMS; SQL 服务器? MYSQL?后GRESQL?

编辑:

如果您需要这种灵活性,请尝试实体属性值模型

You can do it like this but it is NOT RECOMMENDED

CREATE PROCEDURE Osvezi
    -- Add the parameters for the stored procedure here
    @tablica nvarchar(200)
AS
BEGIN
    EXECUTE('SELECT * FROM' + @tablica);
END

Also it would depend on which DBMS you are using; SQL SERVER? MYSQL? POSTGRESQL?

Edit:

If you need this kind of flexibility, try the Entity-Attribute-Value Model

心奴独伤 2024-12-29 20:56:39

您需要在字符串中使用两个 '' 来获得一个 ',并且需要删除 then

IF object_id('Osvezi') IS NULL
EXEC sp_executesql N' 
CREATE PROCEDURE Osvezi
    -- Add the parameters for the stored procedure here
    @tablica nvarchar(200)
AS
BEGIN
   IF @tablica = ''Uporabniki'' SELECT * FROM Uporabniki;

   IF @tablica = ''Dokumenti'' SELECT * from Dokumenti;
END'
GO

You need to use double '' in the string to get one ' and you need to remove then.

IF object_id('Osvezi') IS NULL
EXEC sp_executesql N' 
CREATE PROCEDURE Osvezi
    -- Add the parameters for the stored procedure here
    @tablica nvarchar(200)
AS
BEGIN
   IF @tablica = ''Uporabniki'' SELECT * FROM Uporabniki;

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