SQL ×¢Èë
SQL×¢Èë¾Í²»ÓýéÉÜÁË£¬ÍøÉϺܶࡣÏÂÃæ½éÉÜһϷÀÖ¹
SQL×¢ÈëµÄ·½·¨¡£
ʹÓÃ
quotename º¯ÊýºÍ
sp_executesql
²Î¿¼Èç
ϱí½á¹¹£ºÕâÊÇÒ»¸öÎĵµ±íÀïÃæÓÐһЩ¼òµ¥µÄ×Ö¶ÎÐÅÏ¢
CREATE
TABLE
[dbo]
.
[DocumentInfo]
(
[ID]
[int]
IDENTITY
(
1,
1) primary key
NOT
NULL,--
Ö÷¼ü
[Name]
[varchar]
(
100)
NOT
NULL,--
ÎĵµÃû×Ö
[FunctionID]
[int]
NOT
NULL
£¬
--
¹¦ÄÜ
ID
Íâ¼ü
[TypeInfo]
[nvarchar]
(
2000)
NULL—
ÃèÊöÐÅÏ¢
£©
ÎÒÃÇ¿ÉÄܻᰴ£¬Ö÷¼ü
ID£¬ÎĵµµÄÃû×Ö£¬¹¦ÄÜ
id£¬
ÃèÊöÐÅÏ¢½øÐвéѯ
£¬Õâ¸öÒ»¿´¾ÍÊÇÆ´½Ó
sqlÓï¾ä£¬Èç¹û²»ÓÃÉÏÃæµÄ·½·¨ºÜ¿ÉÄÜÔì³É
sql×¢Èë
ÀýÈçÄãдµÄ´æ´¢¹ý³Ì¿ÉÄÜÈçÏ£º
CREATE
proc
[dbo]
.
[Doc_search]
(
@ID
int
,
@name
varchar
(
10),
@FID
int
,
@Info
nvarchar
(
20)
)
as
begin
declare
@sql
varchar
(
300)
set
@sql
=
'select * from
[DocumentInfo] where 1=1'
if
@ID
<>-
1
set
@sql
=
@sql
+
' and ID='
+
CAST
(
@ID
as
varchar
)
if
@name
<>
''
set
@sql
=
@sql
+
' and name='''
+
CAST
(
@name
as
varchar
)
+
''''
if
@FID
<>-
1
set
@sql
=
@sql
+
' and
[FunctionID]='
+
CAST
(
@FID
as
varchar
)
if
@Info
<>
''
set
@sql
=
@sql
+
' and [TypeInfo] like ''%'
+
CAST
(
@Info
as
varchar
)+
'%'''
print
@sql
end
exec
(
@sql
)
ËäÈ»ÄãÓÃÁË´ø²ÎÊýµÄ´æ´¢¹ý³Ì
£¬µ«±¾ÖÊÉÏ»¹ÊÇÆ´½Ó×Ö·û´®£¬Ã»ÓÐÓÐЧµÄ·ÀÖÎ
sql×¢Èë¡£ÏÖÔÚ
·Ö±ðÓÃ
quotename()º¯ÊýºÍ
sp_executesql¡£
Quotename()º¯Êý²»ÖªµÀÓõÄÇë×ÔÐвéÕÒ£¬
sp_executesqlµÄÓ÷¨
¼ûÉÏһƪ¶¯Ì¬
sql
Quotename¸Ä½øÈçÏ£º
alter
proc
[dbo]
.
[Doc_search]
(
@ID
int
,
@name
varchar
(
10),
@FID
int
,
@Info
nvarchar
(
20)
)
as
begin
declare
@sql
varchar
(
300)
set
@sql
=
'select * from
[DocumentInfo] where 1=1'
if
@ID
<>-
1
set
@sql
=
@sql
+
' and ID='
+
CAST
(
@ID
as
varchar
)
Ïà¹ØÎĵµ£º
SQlÊý¾ÝÀàÐÍ(-_-ѧSQL¾Í´ÓÊý¾ÝÀàÐÍ¿ªÊ¼°É£¡)
bit ÕûÐÍ
ÆäÖµÖ»ÄÜÊÇ0¡¢1»ò¿ÕÖµ¡£
ÕâÖÖÊý¾ÝÀàÐÍÓÃÓÚ´æ´¢Ö»ÓÐÁ½ÖÖ¿ÉÄÜÖµµÄÊý¾Ý£¬ÈçYes »òNo¡¢True »òFalse ¡¢On »òOff.
int ÕûÐÍ 4¸ö×Ö½Ú
´Ó- 2^31(-2147483648)µ½2^31 (2147483 647)Ö®¼äµÄÕûÊý¡£
´æ´¢µ½Êý¾Ý¿âµÄ¼¸ºõËùÓÐÊýÖµÐ͵ÄÊý¾ ......
±àÂë¹ý³ÌÖÐÓöµ½µÄSQL·ÖÒ³Çé¿ö£¬×ܽ᣺
´ÓÊý¾Ý¿â±íÖеÚMÌõ¼Ç¼¿ªÊ¼¼ìË÷NÌõ¼Ç¼
MySQL£º
ÏȲéѯ·ÖÒ³£¬È»ºóÅÅÐò£º
select * from (select * from student limit 5,2) pageTable order by id desc ;
ÏÈÅÅÐò£¬È»ºó²éѯ·ÖÒ³£ºselect * from student order by id desc limit 5,2 ;
Oracle£º
SELECT * fro ......
ALTER function [dbo].[split]
(
@SourceSql varchar(8000),
@StrSeprate varchar(10)
)
returns @temp table(F1 varchar(100))
as
begin
declare @i int
set @SourceSql = rtrim(ltrim(@SourceSql))
set @i = charindex(@StrSeprate,@SourceSql)
while @i >= 1
begin
if len( ......
1. SQLÐÐתÁдúÂë
-
-
±íÀïÃæÓÐÕâôһ×éÊý¾Ý:
code price
'MCU0-3' 15
'MCU0-3' 20
'MCU0-3' 22
'MCU ......