CREATE procedure Up_Test 
@births datetime
as
declare @test varchar(3000)
set @test='select * into test from titles,titleauthor where titles.title_id=titleauthor.title_id and datediff(d,'''+CONVERT(varchar(10),births,120)+''',titles.pubdate)>1'
exec (@test)
GO

解决方案 »

  1.   

    根据pengdali(大力 V3.0) 的提示,
    CREATE procedure Up_Test 
    @births datetime
    as
    declare @test varchar(3000)
    set @test='select * into test from titles,titleauthor where titles.title_id=titleauthor.title_id and datediff(d,CONVERT(varchar(10),@births,120),titles.pubdate)>1'
    exec (@test)
    GO
    又出现下列的错误:
    服务器: 消息 137,级别 15,状态 2,行 1
    必须声明变量 '@births'。
    我的@births是申明了datetime变量的嘛,为什么啊???
      

  2.   

    CREATE procedure Up_Test 
    @births datetime
    as
    declare @test varchar(1000)
    set @test='select * into test from titles,titleauthor where titles.title_id=titleauthor.title_id and datediff(d,'+@births +' , titles.pubdate)>1'
    exec( @test)
    GOexec Up_Test '2001-3-22'
    为什么会抱错:
    从字符串转换为 datetime 时发生语法错误。
      

  3.   

    CREATE procedure Up_Test 
    @births datetime
    as
    declare @test varchar(1000)
    set @test='select * into test from titles,titleauthor where titles.title_id=titleauthor.title_id and datediff(d,'+@births +' , titles.pubdate)>1'
    exec( @test)
    GO
      

  4.   


    CREATE procedure Up_Test 
    @births datetime
    as
    declare @test varchar(3000)
    set @test='select * into test from titles,titleauthor where titles.title_id=titleauthor.title_id and datediff(d,'''+CONVERT(varchar(10),@births,120)+''',titles.pubdate)>1'
    execute (@test)
      

  5.   

    根据pengdali(大力 V3.0) 的提示,
    CREATE procedure Up_Test 
    @births datetime
    as
    declare @test varchar(3000)
    set @test='select * into test from titles,titleauthor where titles.title_id=titleauthor.title_id and datediff(d,CONVERT(varchar(10),@births,120),titles.pubdate)>1'
    exec (@test)
    GO
    又出现下列的错误:
    服务器: 消息 137,级别 15,状态 2,行 1
    必须声明变量 '@births'。
    我的@births是申明了datetime变量的嘛,为什么啊???
    CREATE procedure Up_Test 
    @births datetime
    as
    declare @test nvarchar(3000),@par nvarchar(100)
    select @test='select * into test from titles,titleauthor where titles.title_id=titleauthor.title_id and datediff(d,CONVERT(varchar(10),@births,120),titles.pubdate)>1'
    select @par='@births datetime'
    execute sp_executesql @test,@par,@@births
      

  6.   

    大力的应该可以。
    CREATE procedure Up_Test 
    @births datetime
    as
    declare @test varchar(3000)
    set @test='select * into test from titles,titleauthor where titles.title_id=titleauthor.title_id and datediff(d,CONVERT(varchar(10),@births,120),titles.pubdate)>1'
    exec (@test)
    GO
    又出现下列的错误:
    服务器: 消息 137,级别 15,状态 2,行 1
    必须声明变量 '@births'。
    我的@births是申明了datetime变量的嘛,为什么啊???你的@births变量不能用‘’号引起来。
    也就是:
    set @test='select * into test from titles,titleauthor where titles.title_id=titleauthor.title_id and datediff(d,CONVERT(varchar(10),@births,120),titles.pubdate)>1'
    应改成:
    set @test='select * into test from titles,titleauthor where titles.title_id=titleauthor.title_id and datediff(d,'''+CONVERT(varchar(10),births,120)+''',titles.pubdate)>1'
      

  7.   

    try:
    CREATE procedure Up_Test 
    @births datetime
    as
    declare @test varchar(1000)
    set @test='select * into test from titles,titleauthor where titles.title_id=titleauthor.title_id and datediff(d,'+@births +' , titles.pubdate)>1'
    exec( @test)
    GO