SQL分割逗号的函数和用法
在程序中我们可能经常会遇到这种情况,比如要取一个listbox里面的选择项,得到的结果可能是string ID="id1,id2,id3,id4",然后我们要把这些ID插入到数据库中,同时每个id对应的是要插入一条记录。实现的方法有很多,但是如果我们通过下面这个函数(RecurrentSplit)就能简单的达到上述效果。RecurrentSplit的使用也非常简单。
例如:
select row_number()over(order by indexno desc) as seq ,* from recurrentsplit('1,2,3,4,5,',',',0,0)
这样我们就会分成5条记录,因为我的要求是一个listbox中item[i]其中最小的说明他的排序在最前头,同时排序是但SEQ的降序排列。所以在这个查询中我用row_number()over(order by indexno desc) as seq得出了他的顺序号
如下
seq indexno SplitName
1 4 5
2 3 4
3 2 3
4 1 2
5 0 1
然后就是对这个结果进行操作咯
如
--设置选择商品为推荐并按传入的降序将商品推荐排序
update zp_auction_mst
set ishot=1,hotseq = b.seq
from zp_auction_mst a,
(select row_number()over(order by indexno desc) as seq ,* from recurrentsplit(@AuctionID,',',0,0)) as b
where a.auctionid= b.splitName
搞定,就是这么简单了。。。。
CREATE FUNCTION [dbo].[RecurrentSplit]
(
@nvStr nvarchar(2000) --需要分割字符串
,@vSeparte varchar(50) --分割字符串
,@iIsHaveSeparte int --是否显示字符串
,@iIsBefore int --是否是后面的分割符(分割字符分割的顺序)
)
RETURNS @Split table
(
Ind
相关文档:
SQL Server 2005 创建到 Oracle10g 的链接服务器
由 lwgboy @ MoFun.CC, 在 08-9-12 下午5:00
标记: linkserver, oracle, sqlserver, 链接服务器
SQL Server 2005 创建到 Oracle10g 的链接服务器
SQL Server 2005 异类数据源(ORACLE10G)链接服务器的建立
本文简述SqlServer 2005 链接到 Oracle10g 服务器的过程及基 ......
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[f_convert]') and xtype in (N'FN', N'IF', N'TF'))&n ......
1.修改列
EXEC sp_rename 'TableName.ColumnName','NewColumName'
2.增加列
ALTER TABLE TableName ADD ColumnName int --Type In Here
3.删除列
ALTER TABLE TableName DROP Column ColumnName ......
--该表保存结果
create table c(c1 tinyint, c2 tinyint, c3 tinyint, c4 tinyint, c5 tinyint, c6 tinyint, c7 tinyint, c8 tinyint, c9 tinyint, c10 tinyint, c11 tinyint, c12 tinyint, c13 tinyint, c14 tinyint, c15 tinyint, c16 tinyint, c17 tinyint, c18 tinyint, c19 tinyint, c20 tinyint, c21 tinyint, c22 t ......
select top PageSize * from 表
where 条件 and id not in
(select top PageSize*(CurrentPageIndex-1) id from 表 where 条件 order by 排序条件)
order by 排序条件
《PageSize 是GridView中每页显示的信息条数,PageSize*(CurrentPageIndex-1) 在SQL中不识别,需定义变量来替换》 ......