How To Use Dynamic Sql in Sql Server ?
How To Use Dynamic Sql in Sql Server ?
¶¯Ì¬SQL ÔÚsql server ProcedureÖеÄÓ¦ÓÃ
Create PROCEDURE [dbo].[Proc_Get_Serial_No]
(
@Table_Name varchar(20),
@No_File varchar(20)
)
AS
Declare @Serial_No int
Begin
Declare @Sql nvarchar(max)
Set @Sql ='select @Serial_No= isnull(Max('+@No_File+'),0)+1 from '+@Table_Name+''
Execute sp_executesql @Sql,
N'@Serial_No int output',
@Serial_No output
print @Serial_No
End
---Result---
Serial_No
-----------
1
×¢Ò⣺¶ÔÓÚIntput µÄ²ÎÊýÐèÒªÓà '+@parameter+'
¶ÔÓÚoutput µÄ²ÎÊýÔòÐèÒªÔÚ Ö´Ðж¯Ì¬sql µÄʱ¼äÒÔ¶¨Òå²ÎÊýµÄÐÎʽ˵Ã÷¡£
ÈçÉÏÃæµÄ£º N'@Serial_No int output'£¬È»ºó²ÅÊDzÎÊýÊä³ö,Èç @Serial_No output
--If Your want to output more the one value, your can reference the sql section as below¡£
Note: The Output Parameter Define.
Create PROCEDURE [dbo].[Proc_Get_Serial_No]
(
@Table_Name varchar(20),
@No_File varchar(20)
&n
Ïà¹ØÎĵµ£º
¸ñʽ:
CONVERT(data_type,expression[,style])
˵Ã÷:
´ËÑùʽһ°ãÔÚʱ¼äÀàÐÍ(datetime,smalldatetime)Óë×Ö·û´®ÀàÐÍ(nchar,nvarchar,char,varchar)
Ï໥ת»»µÄʱºò²ÅÓõ½.
Àý×Ó:
SELECT CONVERT(varchar(30),getdate(),101) now
½á¹ûΪ:
now
---------------------------------------
|09/15/2001
===== ......
1¡¢°´Ê±¼äÅÅÐò
select * from tbl_lms_loginhistory order by loginhistory_logintime desc
2¡¢
ÔÚÒ»¸öÊý¾Ý¿âÖÐÊÇÏÂÁеÄÑù×Ó
³µ´Î Éí·ÝÖ¤ºÅ
27 1
27 2
45 1
25 1
45 2
ÏÖÔÚÎÒÏë »ñµÃµÄÊÇ
³µ´Î ³Ë×ø´ÎÊý
25 1
27 3
45 2
ÇëÎÊ£¬ÎÒµÄSQLÓï¾ä¸ÃÔõôд£¿
select ³µ´ ......
The aspnet_Profile table contains the following fields: UserId, PropertyNames, PropertyValuesString, PropertyValuesBinary, and LastUpdatedDate. The PropertyNames field contains a string delimited with colons (:) that identify which profile fields are stored, what their datatype is and their of ......
Ò»¡¢°´Ö¸¶¨·ûºÅ·Ö¸î×Ö·û´®£¬·µ»Ø·Ö¸îºóµÄÔªËØ¸öÊý£¬·½·¨ºÜ¼òµ¥£¬¾ÍÊÇ¿´×Ö·û´®ÖдæÔÚ¶àÉÙ¸ö·Ö¸ô·ûºÅ£¬È»ºóÔÙ¼ÓÒ»£¬¾ÍÊÇÒªÇóµÄ½á¹û¡£
CREATE function Get_StrArrayLength
(
@str varchar(1024), --Òª·Ö¸îµÄ×Ö·û´®
@split varchar(10) --·Ö¸ô·ûºÅ
)
......