佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

楼主: tensaix2j

SQL杂症集

[复制链接]
 楼主| 发表于 22-11-2006 04:28 PM | 显示全部楼层
sql functions有没有办法, trim 掉某character之后的characters..

比如:

abcdefg-h  变 abcdefg
abcd-efgh  变 abcd
回复

使用道具 举报


ADVERTISEMENT

发表于 22-11-2006 05:08 PM | 显示全部楼层
原帖由 天魔神 于 27-10-2006 01:43 AM 发表
请问各位高手,最近小弟我在做一个足球的作业.运用于SQL-PLUS

DBMS_output.put_line('Team  Played  Won  Drew  Lost  GF  GA  GD  Points');
DBMS_output.put_line('----  ------  ---  ----  ----  --  --   ...

我想update就可以了。

原帖由 tensaix2j 于 22-11-2006 04:28 PM 发表
sql functions有没有办法, trim 掉某character之后的characters..

比如:

abcdefg-h  变 abcdefg
abcd-efgh  变 abcd

substring(field, location, lenght)

[ 本帖最后由 hui_wooi 于 22-11-2006 05:10 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 22-11-2006 07:45 PM | 显示全部楼层
原帖由 hui_wooi 于 22-11-2006 05:08 PM 发表

我想update就可以了。


substring(field, location, lenght)



我知道这个function...


问题是
length,location, field
都是variable..


而且我用

select something from tblBlah1 a
full outer join
tblBLah2 b
on
substring(a.field,0,charindex("-",a.field))=substring(b.field,0,charindex("_",b.field))

不行。。因为
有时 "-" 没出现
结果拿了0length的substring

[ 本帖最后由 tensaix2j 于 22-11-2006 07:53 PM 编辑 ]
回复

使用道具 举报

发表于 22-11-2006 07:56 PM | 显示全部楼层
原帖由 tensaix2j 于 22-11-2006 07:45 PM 发表



我知道这个function...


问题是
length,location, field
都是variable..


而且我用

select something from tblBlah1 a
full outer join
tblBLah2 b
on
substring(a.field,0,charindex(&qu ...

你的意思是不一定有“-”?
回复

使用道具 举报

 楼主| 发表于 22-11-2006 07:57 PM | 显示全部楼层
原帖由 hui_wooi 于 22-11-2006 07:56 PM 发表

你的意思是不一定有“-”?



没错。。而且,位置不定
回复

使用道具 举报

 楼主| 发表于 22-11-2006 08:00 PM | 显示全部楼层
等等。。好象可以用case when...
回复

使用道具 举报

Follow Us
发表于 22-11-2006 08:08 PM | 显示全部楼层
原帖由 tensaix2j 于 22-11-2006 08:00 PM 发表
等等。。好象可以用case when...

如何?
能找出string的pattern会比较方便。
回复

使用道具 举报

发表于 22-11-2006 11:48 PM | 显示全部楼层
原帖由 tensaix2j 于 22-11-2006 04:28 PM 发表
sql functions有没有办法, trim 掉某character之后的characters..

比如:

abcdefg-h  变 abcdefg
abcd-efgh  变 abcd


得自己写 sql function 了.
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 23-11-2006 07:41 AM | 显示全部楼层
可以了。。谢谢。。

  1. select distinct(family),b.productname from tblproductlookup a
  2. full outer join [pgsisddev01].j_autouph.dbo.tblProd b
  3. on
  4. case when charindex('-',a.family)>0 then substring(a.family,0,charindex('-',a.family)) else a.family end =
  5. case when charindex('_',b.productname)>0 then substring(b.productname,0,charindex('_',b.productname)) else b.productname end
复制代码
回复

使用道具 举报

 楼主| 发表于 23-11-2006 08:00 AM | 显示全部楼层
原帖由 神仙祖宗 于 5-9-2006 06:50 PM 发表
如果不要用store procedure该如何作?用纯SQL.



stored proc 和纯sql 有何不同?stored proc 不就只是把sql 给存起来吗?
通常,我都是先写纯 sql,用一些暂时的variable test 过了,再转去 parameterized stored proc..


比如 sql如下。。

declare @i int
。。
set @i= testvalue here..
。。
。。(some procedure...)



create procedure aaa
(   @i int
)
as
...
(some procedure)

[ 本帖最后由 tensaix2j 于 23-11-2006 10:45 AM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 23-11-2006 10:51 AM | 显示全部楼层
来来,给各位观顾本楼的网客介绍一个好东西。。

这个叫bulk insert 的东西,可以让你省下不少写parser的功夫。。
http://msdn2.microsoft.com/en-us/library/ms188365.aspx
回复

使用道具 举报

发表于 23-11-2006 01:50 PM | 显示全部楼层
原帖由 tensaix2j 于 23-11-2006 07:41 AM 发表
可以了。。谢谢。。

select distinct(family),b.productname from tblproductlookup a
full outer join .j_autouph.dbo.tblProd b
on
case when charindex('-',a.family)>0 then substring(a.family,0, ...


这样写很长, 应该尝试自行写 function 缩短你的 code.
回复

使用道具 举报

发表于 23-11-2006 01:53 PM | 显示全部楼层
原帖由 tensaix2j 于 23-11-2006 10:51 AM 发表
来来,给各位观顾本楼的网客介绍一个好东西。。

这个叫bulk insert 的东西,可以让你省下不少写parser的功夫。。
http://msdn2.microsoft.com/en-us/library/ms188365.aspx


bulk insert 很容易出错.
回复

使用道具 举报

 楼主| 发表于 25-11-2006 05:29 PM | 显示全部楼层
原帖由 goatstudio 于 23-11-2006 01:50 PM 发表


这样写很长, 应该尝试自行写 function 缩短你的 code.


说的没错。谢谢
回复

使用道具 举报

 楼主| 发表于 25-11-2006 05:29 PM | 显示全部楼层
问问各位
return table 的 fucntion 好用点还是 view 好用点?
回复

使用道具 举报

 楼主| 发表于 29-11-2006 08:47 AM | 显示全部楼层
再介绍一个好东西,
openXML

http://www.sql-server-performance.com/jb_openxml.asp
回复

使用道具 举报


ADVERTISEMENT

发表于 29-11-2006 09:11 AM | 显示全部楼层
原帖由 tensaix2j 于 25-11-2006 05:29 PM 发表
问问各位
return table 的 fucntion 好用点还是 view 好用点?


这两个是不同的东西呢... 用途也不一样...
回复

使用道具 举报

 楼主| 发表于 29-11-2006 09:36 AM | 显示全部楼层
有一个东西我还弄不清楚。

若我有5 个file group, 1 个 mdf。。
vs
我有1 个file group,1 个mdf,4个 ndf,

哪个比较efficient?
回复

使用道具 举报

 楼主| 发表于 29-11-2006 09:45 AM | 显示全部楼层
原帖由 goatstudio 于 29-11-2006 09:11 AM 发表


这两个是不同的东西呢... 用途也不一样...



为何不可以用return table 的function来代替view 呢?

比如:

create view vwA as
select colA,colB from tblA

用以下来代替
create function funcA
(   
--no parameter
)
returns table as
return
(
 select colA,colB from tblA
)

所以
select * from vwA
变可以用以下来代替
select * from funcA()
回复

使用道具 举报

发表于 29-11-2006 11:00 AM | 显示全部楼层
原帖由 tensaix2j 于 29-11-2006 09:45 AM 发表



为何不可以用return table 的function来代替view 呢?

比如:

create view vwA as
select colA,colB from tblA

用以下来代替
create function funcA
(   
--no parameter
)
returns table as ...


问题在于, 你可以在 sql 里直接呼叫 function.
例如:

SELECT functionA(columA) FROM tablename

这是 view 做不到的.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 17-7-2025 12:02 AM , Processed in 0.124994 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表