SELECT CASE
WHEN c.colorder = 1 THEN o.NAME
ELSE ''
END AS 表名,
c.NAME 列名,
t.NAME 取值类型,
g.[value] AS 创建时间
FROM sysobjects o,
dbo.syscolumns c
LEFT OUTER JOIN dbo.systypes t
ON c.xtype = t.xusertype
LEFT OUTER JOIN sys.extended_properties as g
on c.id = g.major_id
and c.colid = g.minor_id
WHERE o.xtype = 'u'
AND c.id = o.id
and o.name = 'table_name'
SELECT table_name
,'drop table if exists vn0c43l.'+ table_name +';'+char(10)+'create table vn0c43l.'+ table_name +'('+char(10)+LEFT(column_names ,LEN(column_names )-2)+char(10)+') partitioned by (ts string)'+char(10)+'stored as orc ;'AS column_names
,'sqoop import '+'--driver com.microsoft.sqlserver.jdbc.SQLServerDriver \' + char(10)+
'--connect "jdbc:sqlserver://ip;username=; password=;database=" \' + char(10) +'--table '+ table_name +' \'+ char(10)+
'--hive-table dwd.'+ table_catalog + '__' + table_name +' \' + char(10)+'--map-column-hive row_version=string \' + char(10) +
'-m 1--hive-overwrite --hive-import --delete-target-dir; ' + char(10)+' if [ "$?" -eq "0" ]; then '+char(10)+' echo " '+ table_name +' ok" >> status.txt '+char(10)+' fi 'as sqoop_script
FROM information_schema.columnsAS extern
CROSSAPPLY(SELECT'`'+ c.NAME +'` '+casewhen charindex('char',t.NAME)>0then'string'when charindex('int',t.NAME)>0then'int'else'double'end+' comment '''+cast(g.[value]asvarchar(2000))+''','+char(10)FROM sysobjects o,
dbo.syscolumns c
LEFTOUTERJOIN dbo.systypes t
ON c.xtype = t.xusertype
LEFTOUTERJOIN sys.extended_properties as g
on c.id = g.major_id
and c.colid = g.minor_id
WHERE o.xtype ='u'AND c.id = o.id
and o.name = extern.table_name
FOR XML PATH('')) pre_trimmed (column_names)where extern.table_catalog ='walmart_manager_recap'and extern.table_name like'rpt%'GROUPBY table_catalog
, table_name
, column_names;
字段列表
select c.NAME +','+char(10)FROM sysobjects o,
dbo.syscolumns c
LEFTOUTERJOIN dbo.systypes t
ON c.xtype = t.xusertype
LEFTOUTERJOIN sys.extended_properties as g
on c.id = g.major_id
and c.colid = g.minor_id
WHERE o.xtype ='u'AND c.id = o.id
and o.name ='表名称'FOR XML PATH('')
where 语句的拼接
declare@whereClausevarchar(2000)-- 建一个临时表select clause into#whereClausefrom(select cast(''asvarchar(2000))as clause
)as a where1<>1-- 将 where 中的条件插入导临时表中iflen(@var)>0begininsertinto#whereClause select ' field {expression} ' + @var end-- 将临时表中的数据做类似 ' and '.join(array) 的操作ifexists(select1from#whereClause)beginset@whereClause=' where '+ stuff((select' and '+ clause from#whereClause for xml path('')),1,4,'')end