SQLServer : EXEC和sp_executesql的区别
http://www.cnblogs.com/xbf321/archive/2008/11/02/1325067.html
USE [DBCordBlood]
GO
/****** Object: StoredProcedure [dbo].[GetIntKey] Script Date: 05/29/2013 10:44:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetIntKey]
@KeyName nvarchar(50),
@KeyValue int OUTPUT
AS
BEGIN
declare @sql nvarchar(max)
SELECT @sql = 'select @KeyValue = MAX(' + [name] + ') + 1 from '+ @KeyName
FROM sys.columns WHERE object_id = object_id(@KeyName) and column_id=1
exec sp_executesql @sql,N'@KeyValue int output',@KeyValue output
if (@KeyValue is null)
set @KeyValue =1
END