存储过程中sql server 2008中的动态列名

时间:2014-01-13 15:08:19

标签: sql sql-server

我可以在存储过程中使用以下语法吗,

set @count = (select count(*) from [dbo].[employee] where @column_name ='T')

1 个答案:

答案 0 :(得分:2)

CREATE PROCEDURE Proc_Name
@Column_Name NVARCHAR(128),
@COUNT INT OUTPUT
AS
BEGIN
 SET NOCOUNT ON;
  DECLARE @Sql NVARCHAR(MAX);

SET @Sql = N'SELECT @count = count(*) from [dbo].[employee] where ' + QUOTENAME(@Column_Name) 
             + N' =''T'''

EXECUTE sp_executesql @Sql
                      ,N'@COUNT INT OUTPUT'
                      ,@COUNT OUTPUT

END