SQL Server 2008 SELECT * FROM @variable?

前端 未结 6 1310
旧时难觅i
旧时难觅i 2020-12-19 03:53

It is possible?

DECLARE @vTableName varchar(50)

SET @vTableName = (SELECT TableName FROM qms_Types WHERE Id = 1)

SELECT * FROM @vTableName
<
6条回答
  •  北海茫月
    2020-12-19 04:03

    Short answer: No.

    Long answer: Noooooooooooooooooooooooooooooooooooooo. Use dynamic SQL if you have to, but if you're structuring your tables in a way where you don't know the table name ahead of time, it might benefit you to rethink your schema.

    Here is a great resource for learning how to use dynamic SQL: The Curse and Blessings of Dynamic SQL

提交回复
热议问题