create proc PROK1
@tablename nvarchar(20),
@fieldname nvarchar(20),
@userName nvarchar(20) output
as
DECLARE @TJ VARCHAR(8)
set @TJ=CONVERT(VARCHAR,DATEPART(YYYY,getDATE()))+ CONVERT(VARCHAR,DATEPART(MM,getDATE()))
set @TJ='PU'+@TJ
DECLARE @baseSql Nvarchar(3000)
set @baseSql='select @userName=MAX(SUBSTRING(@fieldname,9,4)) from '+@tablename+'where left(@fieldname,4)='+@TJ
EXEC sp_executesql @baseSql,N'@userName varchar(80) OUTPUT',@userName OUTPUT
print @userName
GO
--调用
declare @userName nvarchar(10)
exec PROK1 LSPORV,@userName output
select @userName
--
------解决方案--------------------
create proc PROK1
@tablename nvarchar(20),
@fieldname nvarchar(20),
@userName nvarchar(20) output
as
DECLARE @TJ VARCHAR(8)
set @TJ=CONVERT(VARCHAR,DATEPART(YYYY,getDATE()))+ CONVERT(VARCHAR,DATEPART(MM,getDATE()))
set @TJ='PU'+@TJ
DECLARE @baseSql Nvarchar(3000)
set @baseSql='select @userName=MAX(SUBSTRING('+@fieldname+',9,4)) from '+@tablename+' where left(@fieldname,4)='''+@TJ+''''
EXEC sp_executesql @baseSql,N'@userName varchar(80) OUTPUT',@userName OUTPUT
print @userName
GO
--调用
declare @userName nvarchar(10)
exec PROK1 'LSPORV','columnname',@userName output
select @userName
go
drop procedure prok1
------解决方案--------------------
改成这样
--create table LSPORV
--(cgdh nvarchar(12))
--INSERT INTO LSPORV
--SELECT 'PU2012120001' UNION
--SELECT 'PU2012120002' UNION
--SELECT 'PU2012120003'
--GO
ALTER proc PROK1
@tablename nvarchar(20),
@fieldname nvarchar(20),
@userName nvarchar(20) output
as
DECLARE @TJ VARCHAR(8)
set @TJ=CONVERT(VARCHAR,DATEPART(YYYY,getDATE()))+ CONVERT(VARCHAR,DATEPART(MM,getDATE()))
set @TJ='PU'+@TJ
DECLARE @baseSql Nvarchar(3000)
set @baseSql='select @userName=MAX(SUBSTRING('+@fieldname+',9,4)) from '+@tablename+' where left('+@fieldname+',4)='''+@TJ+''''
EXEC sp_executesql @baseSql,N'@userName varchar(80) OUTPUT',@userName OUTPUT
print @userName
GO
--调用
declare @userName nvarchar(10)
exec PROK1 'LSPORV','cgdh',@userName output
select @userName
go