当前位置: 代码迷 >> Sql Server >> SQL拼凑语句,一个参数的写法。试了很多次试不出来
  详细解决方案

SQL拼凑语句,一个参数的写法。试了很多次试不出来

热度:108   发布时间:2016-04-24 23:09:47.0
SQL拼接语句,一个参数的写法。试了很多次试不出来。
日期型:(如2013-03-30)
@basic_date nvarchar(10)
date='''+convert(nvarchar(10),@date,121)+'''

字符型:
@name nvarchar(20)
name='''+@name+'''

数字型(int)的该怎么写呢?

------解决方案--------------------
引用:
@number int
munber='+@number+'

给你个例子:

DECLARE @sql NVARCHAR(128)
DECLARE @int INT 
SET @int=100
SET @sql='select top 1 * from sys.sysprocesses where spid<'+CONVERT (VARCHAR(10),@int)
EXEC (@sql)

/*
spid   kpid   blocked waittype waittime             lastwaittype                     waitresource                                                                                                                                                                                                                                                     dbid   uid    cpu         physical_io          memusage    login_time              last_batch              ecid   open_tran status                         sid                                                                                                                                                                            hostname                                                                                                                         program_name                                                                                                                     hostprocess cmd              nt_domain                                                                                                                        nt_username                                                                                                                      net_address  net_library  loginame                                                                                                                         context_info                                                                                                                                                                                                                                                       sql_handle                                 stmt_start  stmt_end    request_id
  相关解决方案