如何将变量中的动态sql语句与普通语句连接起来
我有一个非常复杂的查询,它将动态构建并保存在变量中。
作为第二部分,我有另一个正常查询,我想在这两者之间进行内部联接。
为了使它更容易一点,这里有一个小例子来说明我的问题。
对于这个小例子,我使用了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用临时表 &将记录转储到其中(来自动态查询)&使用临时表与您拥有的静态查询连接。
进而
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.
And then
您可能走在正确的轨道上,但只要您信任参数的来源并且不暴露于 SQL 注入,您可能只需要在构建 @query 时更改您的选择,例如:
这样,就像您正在构建您的 @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:
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.