select * from(select top {0} a.companyNo as R1,a.carNo as R2,b.BeginTime as R3,b.EndTime as R4,b.SumTime as R5,b.ELongitude as R6,b.ELatitude as R7,b.EPosition as R8 from carreg a, SumCarRunTime b where a.carid=b.carid and a.CarId='{1}' and b.EndTime >= '{2}' and b.BeginTime <= '{3}'order by a.CarId,b.BeginTime) A union all Select * From (select top {0} a.companyNo as R1,a.carNo as R2,b.BeginTime as R3,b.EndTime as R4,b.SumTime as R5,b.Longitude as R6,b.Latitude as R7,b.PositionInfo as R8 from carreg a, SumCarStopTime b where a.carid=b.carid and a.CarId='{1}' and b.EndTime >= '{2}' and b.BeginTime <= '{3}'order by a.CarId,b.BeginTime) B
对应到存储过程中该如何实现呢?
之前只做过单条select语句的,这条语句需要将2条select语句union到一起,就不知道该怎么实现了!
刚开始做ASP.net,请帮忙看看!
谢谢!
5 个解决方案
#1
存储过程中的sql语句跟普通的SQL语句有区别吗?
难道存储过程中写的不是sql语句?
难道存储过程中写的不是sql语句?
#2
+1
#3
存储过程中写的是SQL语句,但是没有UNION功能!
存储过程中语句如下:
SET @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName;--查询语句
IF (@WhereString! = '' or @WhereString!=null)
BEGIN
SET @TotalCountSql=@TotalCountSql + ' where '+ @WhereString;
SET @SqlString =@SqlString+ ' where '+ @WhereString;
END
存储过程中语句如下:
SET @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName;--查询语句
IF (@WhereString! = '' or @WhereString!=null)
BEGIN
SET @TotalCountSql=@TotalCountSql + ' where '+ @WhereString;
SET @SqlString =@SqlString+ ' where '+ @WhereString;
END
#4
select top 1 * from article where ID>@ID
union
select top 1 * from article where ID<@ID order by ID DESC
union
select top 1 * from article where ID<@ID order by ID DESC
#5
纠结,这么长。
#1
存储过程中的sql语句跟普通的SQL语句有区别吗?
难道存储过程中写的不是sql语句?
难道存储过程中写的不是sql语句?
#2
+1
#3
存储过程中写的是SQL语句,但是没有UNION功能!
存储过程中语句如下:
SET @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName;--查询语句
IF (@WhereString! = '' or @WhereString!=null)
BEGIN
SET @TotalCountSql=@TotalCountSql + ' where '+ @WhereString;
SET @SqlString =@SqlString+ ' where '+ @WhereString;
END
存储过程中语句如下:
SET @SqlString = N'(select row_number() over (order by '+ @OrderString +') as rowId,'+@ReFieldsStr+' from '+ @TableName;--查询语句
IF (@WhereString! = '' or @WhereString!=null)
BEGIN
SET @TotalCountSql=@TotalCountSql + ' where '+ @WhereString;
SET @SqlString =@SqlString+ ' where '+ @WhereString;
END
#4
select top 1 * from article where ID>@ID
union
select top 1 * from article where ID<@ID order by ID DESC
union
select top 1 * from article where ID<@ID order by ID DESC
#5
纠结,这么长。