SQL 动态拼接SQL 语句

USE [PMS_UnifiedDB_15]
GO
/****** Object:  StoredProcedure [dbo].[SP_GetLogInfo]    Script Date: 2/11/2015 3:04:13 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Alter date: <Alter Date,,>
-- Description:	<Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[SP_GetLogInfo]
	-- Add the parameters for the stored procedure here
	@id bigint,
	@userRoleInput nvarchar(50)
AS
BEGIN
	SET NOCOUNT ON;
		declare @query nvarchar(max);
		set @query ='select * from dbo.COM_Function where function_label='''+@userRoleInput+'''';
		print @query
		exec sp_executeSql @query
	
END
原文地址:https://www.cnblogs.com/ahghy/p/4286692.html