将我的表名作为 sql 查询中的参数传递

发布于 2024-11-13 11:44:49 字数 603 浏览 3 评论 0原文

我需要查询帮助。在我的查询中,我想将表名作为参数传递。这是我的查询:

SELECT DISTINCT 
    CONVERT (varchar, InspectDateTime) AS 'Inspect Date Time', 
    CONVERT (varchar, SynDateTime) AS 'Sync Date Time', 
    Employee, 
    ROUND(OverAllPercentage, 2) AS Grade 
FROM 
    Table_Name 
WHERE 
    (DATEADD(dd, DATEDIFF(dd, 0, InspectDateTime), 0) 
    BETWEEN 
        DATEADD(dd, DATEDIFF(dd, 0, @From ), 0) AND 
        DATEADD(dd, DATEDIFF(dd, 0, @To ), 0)) 
ORDER BY 
    'Inspect Date Time'

这里我想将 Table_Name 作为参数传递。请注意,此查询已经采用两个参数作为参数,即 "@From" 和 "@To"

I need help with a query. In my query I want to pass my table name as parameter. This is my query:

SELECT DISTINCT 
    CONVERT (varchar, InspectDateTime) AS 'Inspect Date Time', 
    CONVERT (varchar, SynDateTime) AS 'Sync Date Time', 
    Employee, 
    ROUND(OverAllPercentage, 2) AS Grade 
FROM 
    Table_Name 
WHERE 
    (DATEADD(dd, DATEDIFF(dd, 0, InspectDateTime), 0) 
    BETWEEN 
        DATEADD(dd, DATEDIFF(dd, 0, @From ), 0) AND 
        DATEADD(dd, DATEDIFF(dd, 0, @To ), 0)) 
ORDER BY 
    'Inspect Date Time'

Here I want to pass the Table_Name as parameter. Please note that this query is already taking two arguments as parameter, namely "@From" and "@To"

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

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

发布评论

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

评论(4

梦罢 2024-11-20 11:44:49

如果您正在使用 MS SQL,您可以执行以下操作:

CREATE PROCEDURE sp_GetMyStuff
(
    @From datetime,
    @To datetime,
    @TableName nvarchar(100)
)
AS

exec('    
    SELECT DISTINCT 
        CONVERT (varchar, InspectDateTime) AS ''Inspect Date Time'', 
        CONVERT (varchar, SynDateTime) AS ''Sync Date Time'', 
        Employee, 
        ROUND(OverAllPercentage, 2) AS Grade 
    FROM 
        ' + @TableName + ' 
    WHERE 
        (DATEADD(dd, DATEDIFF(dd, 0, InspectDateTime), 0) 
        BETWEEN 
            DATEADD(dd, DATEDIFF(dd, 0, ' + @From + '), 0) AND 
            DATEADD(dd, DATEDIFF(dd, 0, ' + @To + '), 0)) 
    ORDER BY 
        1
');

然后调用它

sp_GetMyStuff '2011-05-05', '2011-06-05', 'TBL_MYTABLE'

If you are working with MS SQL you can do:

CREATE PROCEDURE sp_GetMyStuff
(
    @From datetime,
    @To datetime,
    @TableName nvarchar(100)
)
AS

exec('    
    SELECT DISTINCT 
        CONVERT (varchar, InspectDateTime) AS ''Inspect Date Time'', 
        CONVERT (varchar, SynDateTime) AS ''Sync Date Time'', 
        Employee, 
        ROUND(OverAllPercentage, 2) AS Grade 
    FROM 
        ' + @TableName + ' 
    WHERE 
        (DATEADD(dd, DATEDIFF(dd, 0, InspectDateTime), 0) 
        BETWEEN 
            DATEADD(dd, DATEDIFF(dd, 0, ' + @From + '), 0) AND 
            DATEADD(dd, DATEDIFF(dd, 0, ' + @To + '), 0)) 
    ORDER BY 
        1
');

and then just call it

sp_GetMyStuff '2011-05-05', '2011-06-05', 'TBL_MYTABLE'
李不 2024-11-20 11:44:49

在SQL Server中,如果你想“参数化”表名,你必须使用动态SQL

如果是这样,你必须阅读 Erland的动态 SQL 的诅咒和祝福 作为介绍。

所以基本上,您需要将 SQL 语句构建为字符串,然后执行它。没有其他方法可以在 SQL Server T-SQL 语句中“参数化”表名。

In SQL Server, if you want to "parametrize" the table name, you have to use dynamic SQL

If so, you must read Erland's The Curse and Blessing of dynamic SQL as an intro.

So basically, you need to build up your SQL statement as a string, and then execute it. There is no other way to "parametrize" the table name in a SQL Server T-SQL statement.

栀梦 2024-11-20 11:44:49

好的,假设您使用的是 SQL Server(根据 DATEADDDATEDIFF 函数判断),您需要

  1. 构建一个串联的 sql 命令
    作为字符串(注意不要允许
    SQL注入:即你应该检查
    您的 table_name 变量是
    通过查找有效的表名
    可能的名称形式
    information_schema 并验证
    等)

  2. 使用执行动态sql
    sp_executesqlhttp://msdn.microsoft.com/ en-us/library/ms188001.aspx

OK, assuming you're using SQL Server (judging by the DATEADD and DATEDIFF functions), you'll need to

  1. construct a concatenated sql command
    as string (taking care not to allow
    SQL injection: i.e. you should check
    that your table_name variable is a
    valid table name by looking up
    possible names form
    information_schema and validating
    etc.)

  2. execute your dynamic sql using
    sp_executesql: http://msdn.microsoft.com/en-us/library/ms188001.aspx

黑色毁心梦 2024-11-20 11:44:49

谢谢巴历山德。小修改后的最终查询(将@From,@To转换为varchar)是:

CREATE PROCEDURE sp_GetMyStuff

@TableName VARCHAR(128),
@From DATETIME,
@To DATETIME

AS

DECLARE @sql VARCHAR(4000)
SELECT @sql = 'SELECT DISTINCT CONVERT (varchar, InspectDateTime) AS ''Inspect Date Time'', CONVERT (varchar, SynDateTime) AS ''Sync Date Time'', Employee, ROUND(OverAllPercentage, 2) AS Grade
FROM ' + @TableName + '
WHERE
(DATEADD(dd, DATEDIFF(dd, 0, InspectDateTime), 0) BETWEEN DATEADD(dd, DATEDIFF(dd, 0,'''+ CAST(@From AS VARCHAR(100)) +''' ), 0)
AND DATEADD(dd, DATEDIFF(dd, 0,'''+ CAST(@To AS VARCHAR(100)) +'''), 0))
ORDER BY ''Inspect Date Time'''
EXEC (@sql)

GO

Thanks balexandre. The final query after minor modification(casting @From,@To into varchar) is:

CREATE PROCEDURE sp_GetMyStuff

@TableName VARCHAR(128),
@From DATETIME,
@To DATETIME

AS

DECLARE @sql VARCHAR(4000)
SELECT @sql = 'SELECT DISTINCT CONVERT (varchar, InspectDateTime) AS ''Inspect Date Time'', CONVERT (varchar, SynDateTime) AS ''Sync Date Time'', Employee, ROUND(OverAllPercentage, 2) AS Grade
FROM ' + @TableName + '
WHERE
(DATEADD(dd, DATEDIFF(dd, 0, InspectDateTime), 0) BETWEEN DATEADD(dd, DATEDIFF(dd, 0,'''+ CAST(@From AS VARCHAR(100)) +''' ), 0)
AND DATEADD(dd, DATEDIFF(dd, 0,'''+ CAST(@To AS VARCHAR(100)) +'''), 0))
ORDER BY ''Inspect Date Time'''
EXEC (@sql)

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