使用存储过程来动态调用数据(SELECT)

USE [MyTestDb]
GO
/****** Object:  StoredProcedure [dbo].[PROC_GetChannelList]    Script Date: 04/09/2015 09:55:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<LH>
-- Create date: <2015年4月9日 10:19:32>
-- Description:	<Test>
-- =============================================
ALTER PROCEDURE [proc_findTable]
   @TableName VARCHAR(100),
   @TableColumn VARCHAR(MAX),
   @TableWhere VARCHAR(MAX)
AS
DECLARE @SQL VARCHAR(MAX)

SET @SQL = 'SELECT '+@TableColumn+' FROM '+@TableName
IF @TableWhere != ''
BEGIN
    SET @SQL += ' WHERE 1=1 ' + @TableWhere
END
BEGIN
	EXEC(@SQL)
END

DECLARE @SQLWhere VARCHAR(100)
SET @SQLWhere = ''

exec [proc_findTable] 'MyTestTableOne','*',@SQLWhere

  

posted @ 2015-04-09 10:32  临冰听雪丶  阅读(332)  评论(0编辑  收藏  举报