如何在SQL中编写SELECT语句

问题描述:

你好,

如何通过执行String
从表中选择值

Hi there,

How to select the values from table by executing the String

DECLARE	@Q	As	Varchar(Max)

SELECT	@Q	=
''SELECT	*	FROM	Table1''

EXEC	@Q


我收到错误- Could not find stored procedure '' SELECT * FROM Table1''.

在此先感谢


I get an error - Could not find stored procedure '' SELECT * FROM Table1''.

Thanks in advance

尝试一下.
Try this.
EXEC(''select * from table1'')




Or

DECLARE @Q  As  Varchar(Max)
SET  @Q  = 'SELECT *   FROM    Table1';
EXEC(@Q)


尝试一下.将括号放在EXEC行中
Try this. Put the brackets in EXEC line
DECLARE @strsql AS VARCHAR(50)
SET @strsql = 'SELECT * FROM Orders'
EXEC (@strsql)


您也可以使用sp_executesql方法执行


Also you can use sp_executesql method to execute

sp_executesql N'SELECT * FROM Orders'


检查此 SQL教程 [
Check this SQL Tutorial[^], nice and simple way to start.