SQLServer
: EXECºÍsp_executesqlµÄÇø±ð
ÕªÒª
1,EXECµÄʹÓÃ
2£¬sp_executesqlµÄʹÓÃ
MSSQLΪÎÒÃÇÌṩÁËÁ½ÖÖ¶¯Ì¬Ö´ÐÐSQLÓï¾äµÄÃüÁ·Ö±ðÊÇEXECºÍsp_executesql;ͨ³£,sp_executesqlÔò¸ü¾ßÓÐÓÅÊÆ£¬Ëü
ÌṩÁËÊäÈëÊä³ö½Ó¿Ú£¬¶øEXECûÓС£»¹ÓÐÒ»¸ö×î´óµÄºÃ´¦¾ÍÊÇÀûÓÃsp_executesql£¬Äܹ»ÖØÓÃÖ´Ðмƻ®£¬Õâ¾Í´ó´óÌṩÁËÖ´ÐÐÐÔÄÜ(¶ÔÓÚÕâ¸öÎÒÔÚ
ºóÃæµÄÀý×ÓÖлáÏê¼Ó˵Ã÷)£¬»¹¿ÉÒÔ±àд¸ü°²È«µÄ´úÂë¡£EXECÔÚijЩÇé¿öÏ»á¸üÁé»î¡£³ý·ÇÄúÓÐÁîÈËÐÅ·þµÄÀíÓÉʹÓÃEXEC£¬·ñ²à¾¡Á¿Ê¹ÓÃ
sp_executesql.
1,EXECµÄʹÓÃ
EXECÃüÁîÓÐÁ½ÖÖÓ÷¨£¬Ò»ÖÖÊÇÖ´ÐÐÒ»¸ö´æ´¢¹ý³Ì£¬ÁíÒ»ÖÖÊÇÖ´ÐÐÒ»¸ö¶¯Ì¬µÄÅú´¦Àí¡£ÒÔÏÂËù½²µÄ¶¼ÊǵڶþÖÖÓ÷¨¡£
ÏÂÃæÏÈʹÓÃEXECÑÝʾһ¸öÀý×Ó,´úÂë1
DECLARE
@TableName VARCHAR
(50),@Sql
NVARCHAR(MAX
),@OrderID INT
;
SET
@TableName = 'Orders'
;
SET
@OrderID = 10251;
SET
@sql
= 'SELECT * from '
+QUOTENAME(@TableName) +'WHERE OrderID = '
+CAST
(@OrderID AS
VARCHAR
(10))+' ORDER BY ORDERID DESC'
EXEC
(@sql
);
.csharpcode, .csharpcode pre
{
font-size: small;
colo ......
ÒÉÎÊ£º
1£¬ sqlserverÀïÃæÖ´ÐÐ xp_cmdshell
exec @error=xp_cmdshell 'cmdstr......'
·µ»ØÖµÊÇʲôֵ£¿dosÃüÁîµÄ´íÎó£¿Ê²Ã´ÑùµÄ´íÎó¿ÉÒÔÆË×½µ½£¿
±ÈÈç:
DECLARE @error int
EXEC @error=master.dbo.xp_cmdshell 'bcp CM_DWHSend.dbo.WK_CM_DWHSend_Master in "D:\Sales\DWH_Onl\2010042_SENDMEMM.csv" -n -t, -S(local) -Usa -P '
SELECT @error
µ±·¢ÉúÎļþÄÚÈÝ´íÎó³öÏÖ¡¾Error = [Microsoft][ODBC SQL Server Driver]BCP ¥Çー¥¿¥Õ¥¡¥¤¥ëÖФÇÓèÆÚ¤·¤Ê¤¤ EOF ¤¬検³ö¤µ¤ì¤Þ¤·¤¿¡£¡¿Ê±£¬@error²»ÄÜÈ¡µÃ´íÎ󣬵«ÊÇÎļþ²»´æÔڵȡ¾Error = [Microsoft][ODBC SQL Server Driver]Ò»À¨¥³¥Ôー ¥Û¥¹¥È¤Î¥Çー¥¿¥Õ¥¡¥¤¥ë¤¬開¤±¤Þ¤»¤ó¡£¡¿´íÎóʱ£¬@errorÄÜÈ¡µÃ´íÎó1¡£
2£¬ ´æ´¢¹ý³ÌÖУ¬Ê²Ã´ÑùµÄ´íÎó»áµ¼Ö³ÌÐòÖÕÖ¹£¬²»ÔÙÖ´ÐкóÃæµÄÓï¾ä£¬Ê²Ã´ÑùµÄ´íÎó»á±¨´íºó¼ÌÐøÖ´ÐУ¿
¹Û²ì£º
ÓеĴíÎóÖ»Ìø¹ý´íÎóµÄÓï¾ä¼ÌÐøÖ´ÐУ¬±ÈÈç×Ö·û´®¹ý³¤½Ø¶Ï£»
ÓеĴíÎó»áÖÕÖ¹µ±Ç°ËùÔÚ´æ´¢¹ý³Ì£¬µ«ÊÇ»á¼ÌÐøÖ´Ðд洢¹ý³ÌÍâµÄÓï¾ä£¬±ÈÈçselect Ò»¸ö²»´æÔÚµÄ±í£»
ÓеĴíÎó»áÖÕÖ¹Õû¸ö½ø³Ì£¬±ÈÈçtrucateÒ»¸ö²»´æÔÚµÄ±í¡£ ......
½øÀ´ÒòΪ¿ª·¢ÔÒò£¬¾³£ÐèҪʹÓÃÈÕÆÚ·½ÃæµÄ±È½ÏºÍ²Ù×÷£¬ÕûÀíÁËÒ»ÏÂÕâ·½ÃæµÄ×ÊÁÏ£¬¹©´ó¼Ò¹²Ïí£º
SQL ServerÖÐÎÄ°æµÄĬÈϵÄÈÕÆÚ×Ö¶Îdatetime¸ñʽÊÇyyyy-mm-dd Thh:mm:ss.mmm
ÀýÈç:
select getdate()
2004-09-12 11:06:08.177
ÕûÀíÁËÒ»ÏÂSQL ServerÀïÃæ¿ÉÄܾ³£»áÓõ½µÄÈÕÆÚ¸ñʽת»»·½·¨:
¾ÙÀýÈçÏÂ:
select CONVERT(varchar, getdate(), 120 )
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),@#-@#,@#@#),@# @#,@#@#),@#:@#,@#@#)
20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
select CONVERT(varchar(12) , getdate(), 1 ......
*** author£ºSusan
*** date:2005/08/05
*** expliation:ÈçºÎ寫´æ儲過³ÌµÄ¸ñʽ¼°Àý×Ó£¬ÓÐÓÎ標µÄÓ÷¨£¡
*** ±¾°æ:SQL SERVER °æ£¡
******************************************************/
ÔÚ´æ儲過³ÌÖеĸñʽ規¸ñ£º
CREATE PROCEDURE XXX
/*
ÁÐ舉傳Èë參數
1£ºÃû稱£¬2£º類ÐÍ£¬°üÀ¨長¶È
Eg:@strUNIT_CODE varCHAR(3)
*/
參數1£¬
參數2……………
As
/*
¶¨義內²¿參數
1£ºÃû稱£¬2£º類ÐÍ£¬°üÀ¨長¶È
Eg:@strUNIT_CODE varCHAR(3)
*/
Declare
參數1£¬
參數2……………
/*
³õʼ»¯內²¿參數
Eg:SET @strUNIT_CODE=’’
*/
Set參數1µÄ³õʼֵ
Set參數2µÄ³õʼֵ…………
/*
過³ÌµÄÖ÷內ÈÝ區
Trascation£º這裡Æðµ½µÄ×÷ÓÃÊÇ£¬Èç¹ûËûÖÐ間µÄÈκÎÒ»個執ÐÐ錯誤£¬¾Í ......
˵Ã÷£ºÔÚSupplyPlan±íÖУ¬´æ´¢×Åÿһ¸öRequestQty¼°Æä¶ÔÓ¦µÄ¿ªÊ¼ÖÕÖ¹ÈÕÆڶΣ»ÒòΪÎÒÔÚÒÔºó´¦ÀíÖÐÒªÅжϵ±Ç°ÌìÊôÓÚÄÄÒ»ÌõRequestQtyµÄÈÕÆÚÇø¼ä²¢½øÐд¦Àí£¬ËùÒÔºǫ́Êý¾Ý¿âÖ»ÄÜÉè¼Æ³ÉÕâÖÖ´æ´¢ÐÎʽ£»µ«ÊÇÔÚÒ³ÃæµÄÏÔʾʱºò£¬ÐèÒª¶¯Ì¬µÄ¸ù¾Ýÿһ¸öSupplyPlanNoÉú³É¶ÔÓ¦µÄ¶àÌõÈÕÆÚÇø¶Î¼°ÆäÊýÁ¿ÏÔʾ£¬ËùÒÔ²ÉÓÃ×Ô¶¨Ò庯ÊýÐÎʽ·µ»Ø´¦ÀíÒÔºóµÄ¶¯Ì¬¼Ç¼¼¯£¡·µ»ØµÄ×Ö¶Î@AllRequestQtyΪ"1|13|5|8"ÐÎʽ£¬ÔÚaspÖоͿÉÒÔ²ÉÓÃsplitº¯Êý·Ö×é´Ó¶ø¶¯Ì¬ÏÔʾ×ֶΡ£
/*
** add by wls For: convert many lines of SP datas to one char group by vehicle,part
*/
CREATE FUNCTION xzc.fn_getContent_SupplyPlan
(@s_p_no int = NULL,
@viewFlag varchar(1) = ''
)
RETURNS @fn_getContent_SupplyPlan TABLE
(
ID varchar(8000),
SupplyPlanNo int,
Vehicle varchar(25),
Sort varchar(20),
Part varchar(25),
&n ......
·½Ê½Ò»£º
select top 200 * from view_OrganResource
where 1=1 and OrganID = 57 and
(OrderID not in(select top 1000 OrderID from tb_OrganResource
where 1=1 and OrganID = 57 order by uploadtime desc))
order by uploadtime desc --5858 1980
·½Ê½¶þ£º
select top 200 * from
view_OrganResource where organid=57 and
uploadtime <
(
select min(uploadtime)
from
(
select top 1000 uploadtime from view_OrganResource where organid=57 order by uploadtime desc
) a
)
order by uploadtime desc
·½Ê½Èý£º
SELECT ROW_NUMBER() OVER(ORDER BY orderid DESC) AS 'rnum',*
from view_OrganResource
WHERE 1=1 and rnum >1 and rnum < 1000
SELECT TOP 200 * from (SELECT
ROW_NUMBER() OVER (ORDER BY uploadtime desc) AS RowNumber,
*
from
dbo.view_OrganResource) _myResults
WHERE
&nbs ......