如何从sql server 2008中的子句获取动态 [英] how to get dynamic from clause in sql server 2008
本文介绍了如何从sql server 2008中的子句获取动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好fren,
i在我的查询中有这个小问题
i想让我的from子句动态但是我是从查询本身而不是从字符串中做到的建设者,所以可能的方式我可以解决它。
我的代码看起来像这样
hello frens,
i have this small problem in my query
i want to make my from clause dynamic but i am doing it from the query itself and not from the string builder so what could be the possible way i can solve it out.
my code looks something like this
SELECT abc, ,xyz , NAME
FROM
google g INNER JOIN face f ON g.NetID=2 AND g.NetID=f.NetID
--twitter t INNER JOIN linkedin l ON t.FaceID=2691 AND t.FaceID=l.FaceID
WHERE
NAME LIKE '%abc'
这里我使用不同的表格,这必须是动态的,因为我可以在以后的条件下添加其他表格,同时搜索
请帮帮忙的人
Here i m using different table and this has to be dynamic cuz i can add other table in the later condition while searching
please help out guys
推荐答案
您需要以字符串形式构建查询,然后使用EXEC
或sp_executesql
运行它。
这篇文章有一个很好的解释
< a href =http://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/> http://www.mssqltips.com/sqlservertip/1160/execute- dynamic-sql-commands-in-sql-server / [ ^ ]
You need to build your query up as a string and then useEXEC
orsp_executesql
to run it.
This article has a good explanation
http://www.mssqltips.com/sqlservertip/1160/execute-dynamic-sql-commands-in-sql-server/[^]
CHill60的解决方案1非常好。
我建议使用 SearchBox [ ^ ]。你会发现很多例子。
Solution 1 by CHill60 is very good.
I would suggest to use SearchBox[^]. There you'll find tons of examples.
这篇关于如何从sql server 2008中的子句获取动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文