C
cnhotel
Unregistered / Unconfirmed
GUEST, unregistred user!
在sql查询分析器中执行如下语句正常返回一个表:<br><br>declare @sql varchar(8000)<br>set @sql = 'select djrq as 日期,'<br>select @sql = @sql + 'sum(case zyxm when '''+zyxm+''' <br> then sl else 0 end) as '''+zyxm+''','<br> from (select zyxm from bz_mxjdbb where djh='1') as a<br>select @sql = left(@sql,len(@sql)-1) + ' from bz_mxjdbb where xsdbh=''111'' (注:这里的111我想通过参数传递进去) group by djrq'<br>exec(@sql)<br>go<br><br>我想在sql中通过ADOQuery执行这个语句并使用这个表.现在的做法是:<br><br>1、在sql中创建一个函数get_zyxmmxjdsql (@xsdbh varchar(50)),想通过函数返回完整的sql语句出来再执行查询; <br><br>create function get_zyxmmxjdsql (@xsdbh varchar(50)) Returns varchar(8000) as<br>begin<br> DECLARE @sql varchar(8000)<br> SET @sql = 'select djrq as 日期,'<br> SELECT @sql = @sql + 'sum(case zyxm when ''' + zyxm + ''' <br> then sl else 0 end) as '''<br> + zyxm + ''','<br> FROM (SELECT zyxm<br> FROM bz_mxjdbb<br> WHERE djh = '1') AS a<br> SELECT @sql = LEFT(@sql, len(@sql) - 1) <br> + ' from bz_mxjdbb where xsdbh='+@xsdbh+' group by djrq' <br> return @sql<br>end<br><br>在查询分析器中执行print dbo.get_zyxmmxjdsql ('111')<br>可以看到完整的sql语句内容,但在sql中通过查询分析器中执行<br>select dbo.get_zyxmmxjdsql('111'),能显示出一段sql语句,但是不完整,后面的好像被截断了一样,是不是这个sql语句的长度超出字段的最大值了?<br><br>在delphi中这样写:<br>ADOQuery1.SQL.Text:='select dbo.get_zyxmmxjdsql ('''+TextEdit1.Text+''')';<br>ADOQuery1.Execute;<br>这样执行没提示出错,但在报表文件的数据源中提示N多字段没找到,也就是说在delphi中这样写不行...<br><br>请教各位XDJM我应该怎么办才可以达到我的目的:---执行这个sql并返回一个表