MSSQL
表转实体
功能:指定表名,输出对应的实体
要求:SSMS 执行,列为常见数据类型
declare @TableName sysname = '你的表名'
declare @TableNameProp varchar(200)
select top 1
@TableNameProp=CONVERT(NVARCHAR(100),isnull(g.[value],'-'))
from
sys.tables a left join sys.extended_properties g
on (a.object_id = g.major_id AND g.minor_id = 0)
where a.name=@TableName;
declare @Result varchar(max) = '
/// <summary>
/// ' + @TableNameProp+ +'
/// '+@TableName+'
/// add by dsf '+ convert(varchar(10),GetDate(),23)+'
/// </summary>
public class ' + @TableName + '
{'
select @Result = @Result + '
/// <summary>
/// ' + CONVERT(NVARCHAR(500), ISNULL(ColName, '')) +
'
/// </summary>
public ' + ColumnType + NullableSign + ' ' + ColumnName + ' { get; set; }
'
from
(
SELECT
replace(col.name, ' ', '_') ColumnName,
column_id ColumnId,
prop.value ColName,
case typ.name
when 'bigint' then 'long'
when 'binary' then 'byte[]'
when 'bit' then 'bool'
when 'char' then 'string'
when 'date' then 'DateTime'
when 'datetime' then 'DateTime'
when 'datetime2' then 'DateTime'
when 'datetimeoffset' then 'DateTimeOffset'
when 'decimal' then 'decimal'
when 'float' then 'float'
when 'image' then 'byte[]'
when 'int' then 'int'
when 'money' then 'decimal'
when 'nchar' then 'char'
when 'ntext' then 'string'
when 'numeric' then 'decimal'
when 'nvarchar' then 'string'
when 'real' then 'double'
when 'smalldatetime' then 'DateTime'
when 'smallint' then 'short'
when 'smallmoney' then 'decimal'
when 'text' then 'string'
when 'time' then 'TimeSpan'
when 'timestamp' then 'DateTime'
when 'tinyint' then 'byte'
when 'uniqueidentifier' then 'Guid'
when 'varbinary' then 'byte[]'
when 'varchar' then 'string'
else 'UNKNOWN_' + typ.name
end ColumnType,
case
when col.is_nullable = 1 and typ.name in ('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal', 'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint', 'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
then '?'
else ''
end NullableSign
from sys.columns col
join sys.types typ on
col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
LEFT JOIN sys.extended_properties prop ON col.object_id = prop.major_id AND col.column_id = prop.minor_id
where object_id = object_id(@TableName)
) t
--order by ColumnId
set @Result = @Result + '
}'
print @Result
函数
字符串分割函数
--FUN2:添加字符串分割函数
CREATE FUNCTION Get_strarraystrofindex
(@str VARCHAR(1024),--要分割的字符串
@split VARCHAR(10),--分隔符号
@index INT --取第几个元素
)
returns VARCHAR(1024)
AS
BEGIN
DECLARE @location INT
DECLARE @start INT
DECLARE @next INT
DECLARE @seed INT
SET @str=Ltrim(Rtrim(@str))
SET @start=1
SET @next=1
SET @seed=Len(@split)
SET @location=Charindex(@split, @str)
WHILE @location <> 0
AND @index > @next
BEGIN
SET @start=@location + @seed
SET @location=Charindex(@split, @str, @start)
SET @next=@next + 1
END
IF @location = 0
SELECT @location = Len(@str) + 1
RETURN Substring(@str, @start, @location - @start)
END
GO
链接服务器
创建链接服务器,链接到 Oracle 步骤
① 注册“Oracle Provider for OLE DB”和创建链接服务器
https://blog.csdn.net/weixin_30760895/article/details/98166628
②Win10 配置 Oracle ODBC 数据源[不需要安装 Oracle 客户端]
https://blog.csdn.net/github_38336924/article/details/107042667
③SqlServer 连接 oracle(通过 ODBC)