如何将变量中的动态sql语句与普通语句连接起来

发布于 2024-08-25 06:36:36 字数 1195 浏览 6 评论 0原文

我有一个非常复杂的查询,它将动态构建并保存在变量中。

作为第二部分,我有另一个正常查询,我想在这两者之间进行内部联接。

为了使它更容易一点,这里有一个小例子来说明我的问题。
对于这个小例子,我使用了 AdventureWorks 数据库

一些动态建立的查询

(是的,我知道这里没有什么动态的,因为它只是一个例子。)

DECLARE @query AS varchar(max) ;

set @query = '
select
    HumanResources.Employee.EmployeeID
    ,HumanResources.Employee.LoginID
    ,HumanResources.Employee.Title
    ,HumanResources.EmployeeAddress.AddressID
from
    HumanResources.Employee
    inner join HumanResources.EmployeeAddress
    on HumanResources.Employee.EmployeeID = HumanResources.EmployeeAddress.EmployeeID
;';

EXEC (@query);

我拥有的普通查询

select
    Person.Address.AddressID
    ,Person.Address.City
from
    Person.Address

也许是我想要的但不起作用

select
    @query.*
    ,Addresses.City
from
    @query as Employees
    inner join
    (
        select
            Person.Address.AddressID
            ,Person.Address.City
        from
            Person.Address
    ) as Addresses
    on Employees.AddressID = Addresses.AddressID

I have a quite complicated query which will by built up dynamically and is saved in a variable.

As second part i have another normal query and i'd like to make an inner join between these both.

To make it a little more easier here is a little example to illustrate my problem.
For this little example i used the AdventureWorks database.

Some query built up dynamically

(Yes, i know there is nothing dynamic here, cause it's just an example.)

DECLARE @query AS varchar(max) ;

set @query = '
select
    HumanResources.Employee.EmployeeID
    ,HumanResources.Employee.LoginID
    ,HumanResources.Employee.Title
    ,HumanResources.EmployeeAddress.AddressID
from
    HumanResources.Employee
    inner join HumanResources.EmployeeAddress
    on HumanResources.Employee.EmployeeID = HumanResources.EmployeeAddress.EmployeeID
;';

EXEC (@query);

The normal query i have

select
    Person.Address.AddressID
    ,Person.Address.City
from
    Person.Address

Maybe what i'd like to have but doesn't work

select
    @query.*
    ,Addresses.City
from
    @query as Employees
    inner join
    (
        select
            Person.Address.AddressID
            ,Person.Address.City
        from
            Person.Address
    ) as Addresses
    on Employees.AddressID = Addresses.AddressID

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

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

发布评论

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

评论(2

新一帅帅 2024-09-01 06:36:36

使用临时表 &将记录转储到其中(来自动态查询)&使用临时表与您拥有的静态查询连接。

set @query = 'CREATE table #myTempTable AS
select
    HumanResources.Employee.EmployeeID
    ,HumanResources.Employee.LoginID
    ,HumanResources.Employee.Title
    ,HumanResources.EmployeeAddress.AddressID
from
    HumanResources.Employee
    inner join HumanResources.EmployeeAddress
    on HumanResources.Employee.EmployeeID = HumanResources.EmployeeAddress.EmployeeID
;';

EXEC (@query);

进而

select
    Employees.*
    ,Addresses.City
from
    #myTempTable as Employees
    inner join
    (
        select
            Person.Address.AddressID
            ,Person.Address.City
        from
            Person.Address
    ) as Addresses
    on Employees.AddressID = Addresses.AddressID

Use temp tables & have the records dumped into it (from the dynamic query) & use the temp table to join with the static query that you have.

set @query = 'CREATE table #myTempTable AS
select
    HumanResources.Employee.EmployeeID
    ,HumanResources.Employee.LoginID
    ,HumanResources.Employee.Title
    ,HumanResources.EmployeeAddress.AddressID
from
    HumanResources.Employee
    inner join HumanResources.EmployeeAddress
    on HumanResources.Employee.EmployeeID = HumanResources.EmployeeAddress.EmployeeID
;';

EXEC (@query);

And then

select
    Employees.*
    ,Addresses.City
from
    #myTempTable as Employees
    inner join
    (
        select
            Person.Address.AddressID
            ,Person.Address.City
        from
            Person.Address
    ) as Addresses
    on Employees.AddressID = Addresses.AddressID
潇烟暮雨 2024-09-01 06:36:36

您可能走在正确的轨道上,但只要您信任参数的来源并且不暴露于 SQL 注入,您可能只需要在构建 @query 时更改您的选择,例如:

parameter to your function  '@YourAlternateTableParm'

DECLARE @query AS varchar(max) ; 

set @query = 'select ' + @YourAlternateTableParm 
  + '.*, Addresses.City
from ' + @YourAlternateTableParm
  + ' as Employees 
    inner join 
    ( ..... '

这样,就像您正在构建您的 @query 一样原始字符串,您正在将参数的实际值构建到函数/过程调用中,并使用所需的表名称来表示您的“Employees”文件到字符串中,然后执行它。 SQL 不会按照您尝试的方式动态解释 @query 内联。

You might be on the right track, but as long as you trust the source of the parameter and not exposed for SQL-Injection, you might just have to alter your select when building your @query like:

parameter to your function  '@YourAlternateTableParm'

DECLARE @query AS varchar(max) ; 

set @query = 'select ' + @YourAlternateTableParm 
  + '.*, Addresses.City
from ' + @YourAlternateTableParm
  + ' as Employees 
    inner join 
    ( ..... '

This way, like you were building your original string, you are building the actual value of the parameter to a function/procedure call with the table name desired to represent your "Employees" file into the string, then you execute that. SQL is not dynamically interpretting the @query inline the way you were trying to do.

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