首页 > 代码库 > 如何创建存储过程

如何创建存储过程

IF OBJECT_ID (‘p_GetData‘,‘p‘) IS NOT NULL
DROP PROCEDURE p_GetData;
GO

CREATE PROCEDURE p_GetData
-- Add the parameters for the stored procedure here
@SourceID varchar(255),
@Keyword varchar(255)=‘‘,
@Keyword1 varchar(255)=‘‘,
@Keyword2 varchar(255)=‘‘,
@Keyword3 varchar(255)=‘‘,
@Keyword4 varchar(255)=‘‘,
@Keyword5 varchar(255)=‘‘,
@Keyword6 varchar(255)=‘‘,
@sOrder varchar(255)=‘‘,
@sort varchar(255)=‘‘,
@startRow int=‘‘,
@endRow int=‘‘
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here

declare @sSql varchar(1000)

if @sOrder!=‘‘
begin
  set @sOrder=‘ order by ‘+@sOrder
end

if @sort!=‘‘
begin
  set @sOrder=@sOrder+‘ ‘+@sort
end

if object_id(‘tempdb..##test‘) is not null
begin
  drop table ##test
end

if @SourceID=‘test1‘
begin
  set @sSql=‘SELECT IDENTITY(int,1,1) as rowId,* into ##test FROM test WHERE test like ‘‘%‘+@Keyword+‘%‘‘ ‘+@sOrder
  exec (@sSql)
  select * from ##test where rowId between @startRow and @endRow
end

if @SourceID=‘comp‘
begin
  set @sSql=‘select IDENTITY(int,1,1) as rowId,test as text into ##test from system_table‘
  exec (@sSql)
  select * from ##test
end

if @SourceID=‘login‘
begin
  set @sSql=‘‘
  if @Keyword=‘‘
  begin
    set @sSql=‘useridempty‘
  end
  if @Keyword1=‘‘
  begin
    set @sSql=‘passwordempty‘
  end

  print @sSql
  if @sSql=‘‘
  begin
    print ‘123‘
    --select * from USERFLM where userid=‘sa‘ and pwd=‘123‘

  if not exists(select * from USERFLM where userid=@Keyword and pwd=@Keyword1)
  begin
    set @sSql=‘error‘
  end
end
print @sSql
select @sSql as result

end

-- exec (@sSql)
--
-- if @startRow!=‘‘ and @endRow!=‘‘
-- begin
-- select * from ##test where rowId between @startRow and @endRow
-- end
-- else
-- begin
-- select * from ##test
-- end

END
GO

如何创建存储过程