作为一个"浸淫" Oracle
数据库很久的人来说, 突然转入 T-SQL
, 也就是 MSSQL
, 工作中经常用的查询和 MSMS
使用备忘如下 :
字符串的处理拼接
- 2017-12-8
-
T-SQL
中 引号中 两个引号代表一个引号 - 传入参数需要``区域表示 , 而内容也包括引号, 此时两个单引号表示一个引号, 这种表述在存储过程字符串拼接中也生效. 例如:
EXEC dbo.sp_OutputData
@tablename = 'W1ArchiveItem' , -- sysname
@Where = 'ArchiveItemID=''9C0D8E88-543B-4E7D-8D89-8B7321469B79''' , -- varchar(4000)
@OrderBy = '' , -- varchar(4000)
@IfNotExistsColumn = '' -- varchar(4000)
set @ExSQL = '
SELECT a.WageTableItemID
,a.[WageTableID]
,a.[ArchiveID]
,b.ArchiveNO '
+ case when len(@AdtnFldList) > 1 then ',' + char(10) + @AdtnFldList else '' end
+ char(10)
+ ' from [W1WageTableItem] a
inner join [W1Archive] b on A.ArchiveID = b.ArchiveID
where a.WageTableID = ''' + @p_WageTableID + '''
and (b.ArchiveNO like ''%' + @p_KeyWords + '%'' or b.EmployeeName like ''%' + @p_KeyWords + '%'')'
- 一篇有价值的参考网址; http://blog.51cto.com/qianzhang/1204630
其他常用
--1. 切换对应的库连接
USE CYBPMCOSTDB1041_Dev
--2. 查看存储过程内容
SP_HELPTEXT 'V0DataDict'
--3. 查看表列内容
sp_columns 'A1Org'
--4. 查看数据库表描述, 行列转换过, 可以看到包括备注之类(自定义视图, 后面附视图内容)
SELECT *FROM dbo.V0DataDict WHERE name='A1Org'
--5.
--执行带输出参数的存储过程
DECLARE @p_RecTotal int
EXEC PM1SpecialEqptRegisterPage
@p_OrgID = 'ED6989D2-B7CB-478B-9E37-D0C15D720D6A',
@p_ManageCode = '',
@p_FilterStr='',
@p_PageIndex = 0,
@p_PageSize= 25,
@p_OrderField = 'BizDate',
@p_OrderType = 1,
@p_IsLargeEqpt = -1,
@p_RecTotal = @p_RecTotal OUT
SELECT @p_RecTotal
- 附上对应视图的内容
SET QUOTED_IDENTIFIER ON;
SET ANSI_NULLS ON;
GO
CREATE VIEW [V0DataDict]
AS
SELECT TOP 100 PERCENT
*
FROM ( SELECT ISNULL(CAST(UserTable.value AS VARCHAR(256)), '') AS 表名 ,
REPLACE(REPLACE(REPLACE(CAST(UserTableColumn.value AS VARCHAR(256)),
' ', ''), CHAR(13), ''),
CHAR(10), '') AS 列名 ,
UserTableColumn.colname AS 列编码 ,
CAST(UserTableColumn.colid AS VARCHAR(10)) AS 列序 ,
UserTableColumn.IsPKey AS 主键 ,
UserTableColumn.typename AS 数据类型 ,
CAST(UserTableColumn.length AS VARCHAR(10)) AS 宽度 ,
CASE WHEN UserTableColumn.typename NOT IN ( 'decimal',
'numeric',
'money',
'smallmoney',
'float', 'real' )
THEN ''
ELSE CAST(ISNULL(UserTableColumn.scale, '') AS VARCHAR(10))
END AS 小数位 ,
CASE WHEN UserTableColumn.typename NOT IN ( 'decimal',
'numeric',
'money',
'smallmoney',
'float', 'real' )
THEN ''
ELSE CAST(ISNULL(UserTableColumn.prec, '') AS VARCHAR(10))
END AS 精度 ,
ISNULL(CONVERT(VARCHAR(256), UserTableColumn.text), '') AS 默认值 ,
ISNULL(CASE CONVERT(BIT, ( UserTableColumn.status & 8 ))
WHEN 1 THEN '是'
ELSE ''
END, '') AS 可空 ,
ISNULL(CASE CONVERT(BIT, ( UserTableColumn.status
& 0x80 ))
WHEN 1 THEN '是'
ELSE ''
END, '') AS 自增长 ,
UserTable.name ,
3 AS ListOrder ,
UserTableColumn.colid
FROM ( SELECT sys.sysobjects.id ,
sys.sysobjects.name ,
sys.extended_properties.value
FROM sys.sysobjects
LEFT OUTER JOIN sys.extended_properties ON sys.extended_properties.major_id = sys.sysobjects.id
AND sys.extended_properties.minor_id = 0
WHERE sys.sysobjects.type = 'U' /*and
convert( varchar(10), sys.sysobjects.crdate, 121 ) = convert( varchar(10), getdate(), 121 )*/
) UserTable
INNER JOIN ( SELECT sys.syscolumns.id ,
sys.syscolumns.colid ,
sys.syscolumns.name AS colname ,
sys.extended_properties.value ,
sys.systypes.name AS typename ,
sys.syscolumns.length ,
sys.syscolumns.scale ,
sys.syscolumns.prec ,
sys.syscomments.text ,
sys.syscolumns.status ,
PKeyColumns.object_id ,
PKeyColumns.column_id ,
CASE WHEN PKeyColumns.column_id IS NULL
THEN ''
ELSE '是'
END AS IsPKey
FROM sys.syscolumns
LEFT OUTER JOIN sys.syscomments ON sys.syscolumns.cdefault = sys.syscomments.id
LEFT OUTER JOIN sys.systypes ON ( sys.syscolumns.usertype = sys.systypes.usertype )
AND ( sys.syscolumns.xusertype = sys.systypes.xusertype )
LEFT OUTER JOIN sys.extended_properties ON sys.extended_properties.major_id = sys.syscolumns.id
AND sys.extended_properties.minor_id = sys.syscolumns.colid
LEFT OUTER JOIN ( SELECT
sys.index_columns.object_id ,
sys.index_columns.column_id
FROM
sys.index_columns
INNER JOIN sys.indexes ON sys.indexes.index_id = sys.index_columns.index_id
AND sys.indexes.object_id = sys.index_columns.object_id
WHERE
sys.indexes.is_primary_key = 1
) PKeyColumns ON PKeyColumns.object_id = sys.syscolumns.id
AND PKeyColumns.column_id = sys.syscolumns.colid
) UserTableColumn ON UserTableColumn.id = UserTable.id
UNION ALL
SELECT TOP 100 PERCENT
'' AS 表名 ,
ISNULL(CAST(sys.extended_properties.value AS VARCHAR(256)),
'') AS 列名 ,
sys.sysobjects.name AS 列编码 ,
'' AS 列序 ,
'' AS 主键 ,
'' AS 数据类型 ,
'' AS 宽度 ,
'' AS 小数位 ,
'' AS 精度 ,
'' AS 默认值 ,
'' AS 可空 ,
'' AS 自增长 ,
sys.sysobjects.name ,
1 AS ListOrder ,
0 AS colid
FROM sys.sysobjects
LEFT OUTER JOIN sys.extended_properties ON sys.extended_properties.major_id = sys.sysobjects.id
AND sys.extended_properties.minor_id = 0
WHERE sys.sysobjects.type = 'U' --and
--convert( varchar(10), sys.sysobjects.crdate, 121 ) = convert( varchar(10), getdate(), 121 )
UNION ALL
SELECT TOP 100 PERCENT
'表名' AS 表名 ,
'列名' AS 列名 ,
'列编码' AS 列编码 ,
'列序' AS 列序 ,
'主键' AS 主键 ,
'数据类型' AS 数据类型 ,
'宽度' AS 宽度 ,
'小数位' AS 小数位 ,
'精度' AS 精度 ,
'默认值' AS 默认值 ,
'可空' AS 可空 ,
'自增长' AS 自增长 ,
sys.sysobjects.name ,
2 AS ListOrder ,
0 AS colid
FROM sys.sysobjects
LEFT OUTER JOIN sys.extended_properties ON sys.extended_properties.major_id = sys.sysobjects.id
AND sys.extended_properties.minor_id = 0
WHERE sys.sysobjects.type = 'U' --and
--convert( varchar(10), sys.sysobjects.crdate, 121 ) = convert( varchar(10), getdate(), 121 )
UNION ALL
SELECT TOP 100 PERCENT
'' AS 表名 ,
'' AS 列名 ,
'' AS 列编码 ,
'' AS 列序 ,
'' AS 主键 ,
'' AS 数据类型 ,
'' AS 宽度 ,
'' AS 小数位 ,
'' AS 精度 ,
'' AS 默认值 ,
'' AS 可空 ,
'' AS 自增长 ,
sys.sysobjects.name ,
4 AS ListOrder ,
0 AS colid
FROM sys.sysobjects
LEFT OUTER JOIN sys.extended_properties ON sys.extended_properties.major_id = sys.sysobjects.id
AND sys.extended_properties.minor_id = 0
WHERE sys.sysobjects.type = 'U' /*and
convert( varchar(10), sys.sysobjects.crdate, 121 ) = convert( varchar(10), getdate(), 121 ) */
) A
WHERE A.name LIKE '[A-Z][0-9]%'
ORDER BY A.name ASC ,
A.ListOrder ASC ,
A.colid ASC;
GO
- 另外发现很多人或者我这种半路开始使用MSSQL的经常误区. 如图所示:
- 一般来说刚打开自动登陆的就是对象浏览器. 至于当前界面查询使用哪个链接 是分开的. 不然一直在对象浏览器里面切来切去, 是跟当前查询没有任何关系的.