由於從欄位抓出的值 雖然是由逗點分隔 但整體資料容易被當成一組單一字串
例如
Table
#SomeTable
欄位 :
Id MemberIds
值
1 12,30,58
2 45,68,88
想要篩選 where member_Id in (select MemberIds from SomeTable where Id = 2)
會被當成 Id in ('45,68,88') 文字跟整數無法被篩選
解決方法之一
create table #SomeTable (Id int,MemberIds nvarchar(30))
insert into #SomeTable (Id,MemberIds) values (1,'12,30,58')
insert into #SomeTable (Id,MemberIds) values (2,'45,68,88')
select * from #SomeTable
create table #TargetTable (Id int,member_Id nvarchar(30))
insert into #TargetTable (Id,member_Id ) values (1,12)
insert into #TargetTable (Id,member_Id ) values (2,30)
insert into #TargetTable (Id,member_Id ) values (3,45)
insert into #TargetTable (Id,member_Id ) values (4,58)
insert into #TargetTable (Id,member_Id ) values (5,68)
insert into #TargetTable (Id,member_Id ) values (6,88)
select * from #TargetTable
DECLARE @MemberIds nvarchar(200);DECLARE @SQLString nvarchar(200);
select @MemberIds =MemberIds from #SomeTable(nolocK) where Id = 2
SET @SQLString = N' select * from #TargetTable where member_Id in (' + @MemberIds + ')' ;
EXECUTE sp_executesql @SQLString
將整體都當成字串 就可以執行了
如果用逗點隔開如果用逗點隔開的是字串
如:
Hi,Im,String
可以先把它加上單引號
select '''' + Replace(MemberIds ,',','''' +',' + '''') + '''' from #SomeTable
sean, 2017/6/12 下午 12:09:48
--抓所有的 Table
Select * from INFORMATION_SCHEMA.TABLES
--抓所有的 COLUMNS
Select * from INFORMATION_SCHEMA.COLUMNS
--抓欄位的 Description
select
st.name [Table],
sc.name [Column],
sep.value [Description]
from sys.tables st
inner join sys.columns sc on st.object_id = sc.object_id
left join sys.extended_properties sep on st.object_id = sep.major_id
and sc.column_id = sep.minor_id
and sep.name = 'MS_Description'
where st.name = 'TableName'
and sc.name = 'ColumnName'
--修改欄位的 Description.
EXEC sp_updateextendedproperty
@name = N'MS_Description', @value = 'Your description',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'TableName',
@level2type = N'Column', @level2name = 'Name';
EXEC sp_addextendedproperty
@name = N'MS_Description', @value = 'Code description',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'TableName',
@level2type = N'Column', @level2name = 'ColumnName';
--新增 Table 的 extendedproperty
EXEC sp_addextendedproperty
@name = N'Description', @value = 'Hey, here is TableName description!',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'TableName'
GO
--修改 Table 的 extendedproperty
EXEC sp_updateextendedproperty
@name = N'Description', @value = 'Hey, here is my description! 123',
@level0type = N'Schema', @level0name = 'dbo',
@level1type = N'Table', @level1name = 'TableName'
GO
--讀取 Extended Property
SELECT sys.objects.name AS TableName, ep.name AS PropertyName,
ep.value AS Description
FROM sys.objects
CROSS APPLY fn_listextendedproperty(default,
'SCHEMA', schema_name(schema_id),
'TABLE', name, null, null) ep
WHERE sys.objects.name NOT IN ('sysdiagrams')
ORDER BY sys.objects.name
--讀取 Column 的 Description
SELECT objtype, objname, name, value
FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'TableName', 'column', default);
GO
--讀取特定 Table 的 Description
SELECT *
FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', 'TableName', default, default);
GO
--讀取 所有 Table 的 Description
SELECT *
FROM fn_listextendedproperty (NULL, 'schema', 'dbo', 'table', default, default, default);
GO
--新增或修改資料表說明
IF not exists(SELECT * FROM ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table', '資料表名稱', NULL, NULL))
BEGIN
exec sp_addextendedproperty 'MS_Description', '資料表說明', 'user', 'dbo', 'table', '資料表名稱'
END
ELSE
BEGIN
exec sp_updateextendedproperty 'MS_Description', '資料表說明', 'user', 'dbo', 'table', '資料表名稱'
END
--新增或修改欄位說明
IF not exists(SELECT * FROM ::fn_listextendedproperty (NULL, 'user', 'dbo', 'table', '資料表名稱', 'column', '欄位名稱'))
BEGIN
exec sp_addextendedproperty 'MS_Description', '欄位說明', 'user', 'dbo', 'table', '資料表名稱', 'column', '欄位名稱'
END
ELSE
BEGIN
exec sp_updateextendedproperty 'MS_Description', '欄位說明', 'user', 'dbo', 'table', '資料表名稱', 'column', '欄位名稱'
END
Bike, 2016/6/29 下午 04:42:25