高手帮我看看这个SQL 语句,急啊~!!!(100分)

  • 主题发起人 主题发起人 noelse520
  • 开始时间 开始时间
N

noelse520

Unregistered / Unconfirmed
GUEST, unregistred user!
declare @D DateTime, &nbsp;@Datestr Char(10), @SENSORID Char(5), @TBName Char(14)<br>Select @D = GetDate() /*取系统时间*/<br>select &nbsp; @Datestr = &nbsp;Substring(Convert(char(10),@D),7,4) + Substring(Convert(char(10),@D),1,2) + Substring(Convert(char(10),@D),4,2) &nbsp;<br>declare SiteCursor Cursor for<br> &nbsp;select SENSORID from TBSENSORA order by SENSORID for Read only<br>Open SiteCursor<br>while (0=0) Begin<br> &nbsp;Fetch Next<br> &nbsp; &nbsp;from SiteCursor<br> &nbsp; &nbsp;into @SENSORID <br> &nbsp;if (@@Fetch_Status &lt;&gt; 0) Break<br> &nbsp;select @TBName = 'T' + @SENSORID + @DateStr<br> &nbsp;if not Exists(Select @TBName from sysobjects) /*表不存在就建表*/<br> &nbsp;Create Table @TBName(pk Decimal IDENTITY(1,1) not null primary key, SENSORID varchar(5) not null, CURVALUE float not null, CurTime DateTime not null, WARNED int, USERID varchar(4),CLASSID varchar(6)); <br> &nbsp;print @TBName<br>end<br>Close SiteCursor<br>Deallocate SiteCursor<br><br> &nbsp;@TBName语法错误,如果把@TBName改成一个表名而不用变量就没问题。但我是动态创建表,请问这个问题怎么解决?
 
可以用动态SQL,比如:<br>declare @df varchar(10) <br>declare @str varchar(200) <br>select @df='table1' &nbsp;//表名 <br>select @str='select * from '+@df <br>exec(@str) <br>將@df定義成輸入參數,先賦值。
 
建表部分修改成这样吧!<br>declare @SQLTEXT varchar(2000), @TBName varchar(20)<br>set @TBName = 'TestAA'<br>set @SQLTEXT = ' Create Table ' + &nbsp;@TBName + '(pk Decimal IDENTITY(1,1) not null primary key, SENSORID varchar(5) not null)'<br>exec(@SQLTEXT)
 
Create Function CreateTb(@Str varchar(20))<br> &nbsp;Return int<br> &nbsp;as<br> &nbsp;begin <br> &nbsp; &nbsp;declare @SQLStr Char(300), declare @TName Char(20)<br> &nbsp; &nbsp;if not Exists(Select @TBName from sysobjects) begin <br> &nbsp; &nbsp; &nbsp;select @SQLStr = 'Create Table' + @TName +'(pk Decimal IDENTITY(1,1) not null primary key, SENSORID varchar(5) not null, CURVALUE float not null, CurTime DateTime not null, WARNED int, USERID varchar(4),CLASSID varchar(6))'; <br> &nbsp; &nbsp; &nbsp;exec(@SQLStr)<br> &nbsp; &nbsp; &nbsp;Return 1<br> &nbsp; &nbsp;end <br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp;Return 0<br> &nbsp;end Function<br>刚才那个问题解决了,但又遇到新的问题了,我定义函数,可是老是报错,但我感觉没错啊
 
大哥哥们,我急死了~!
 
--Return int 修改为 Returns int
 
但是还报错,说定义变量有错~!
 
exec(@SQLStr) 哈哈, 刚才我测试了一下函数内不能执行的语句<br>赶快换成存储过程吧
 
Create procedure SP_CreateTable @TBName varchar(20), &nbsp;@Result int output<br> &nbsp;as<br> &nbsp;begin <br> &nbsp; &nbsp;declare @SQLStr varChar(2000)<br> &nbsp; &nbsp;if not Exists(Select 1 &nbsp;from sysobjects where name = @TBName and Type = 'U' ) <br> &nbsp; &nbsp;begin <br> &nbsp; &nbsp; &nbsp;select @SQLStr = 'Create Table &nbsp;' + @TBName +'(pk Decimal IDENTITY(1,1) not null primary key, SENSORID varchar(5) not null, CURVALUE float not null, CurTime DateTime not null, WARNED int, USERID varchar(4),CLASSID varchar(6))'; <br> &nbsp; &nbsp; &nbsp;exec(@SQLStr)<br> &nbsp; &nbsp; &nbsp;if @@Error = 0 set @Result = 1<br> &nbsp; &nbsp; &nbsp;Return @Result<br> &nbsp; &nbsp;end &nbsp;else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;set @Result = 0<br> &nbsp; &nbsp; &nbsp;Return @Result<br> &nbsp; &nbsp;end<br> &nbsp;end <br><br>--调用例子<br>declare @Resulet int<br>exec SP_CreateTable 'TestBB', @Resulet output<br>print @Resulet<br>--可以根据@Resulet得到跟函数一样的返回值<br>--上面语句已经测试通过了,你自己试试
 
你这个是存储过程,但我要用函数~!
 
在不呢? 帮我继续回答问题吧,我急得很,大哥,我以前都是在程序中用SQL,很少写存储过程
 
if not Exists(Select @TBName from sysobjects) /*表不存在就建表*/<br> &nbsp;Create Table @TBName(pk Decimal IDENTITY(1,1) not null primary key, SENSORID varchar(5) not null, CURVALUE float not null, CurTime DateTime not null, WARNED int, USERID varchar(4),CLASSID varchar(6)); <br><br>修改为如下语句:<br> &nbsp;if not Exists(Select @TBName from sysobjects) /*表不存在就建表*/<br> &nbsp;exec('Create Table' + @TBName + '(pk Decimal IDENTITY(1,1) not null primary key, SENSORID varchar(5) not null, CURVALUE float not null, CurTime DateTime not null, WARNED int, USERID varchar(4),CLASSID varchar(6))')
 
if not Exists(Select @TBName from sysobjects) /*表不存在就建表*/<br>这个肯定不行<br>if not exists(select * from sysobjects where name=@tbname and type='U' )<br>剩下的按照楼上的改应该就可以。
 
都说在函数里面exec(@SQLStr) 不能执行这样的语句了。 此路不通肯定要找路拉。<br>我已经指给走了,还不走。 呵呵
 
后退
顶部