DECLARE @ListXML VARCHAR(MAX),@TableName VARCHAR(50)
SET @ListXML=''
SET @TableName='T_SAMPLE'-- MENTION UR TABLE NAME HERE
SELECT @ListXML = (SELECT CASE WHEN DATA_TYPE IN('datetime','smalldatetime') THEN 'ISNULL(CONVERT(VARCHAR(10),'+CONVERT(VARCHAR,COLUMN_NAME)+',101),''''),'
WHEN DATA_TYPE IN('bit','int') THEN 'ISNULL(CAST('+CONVERT(VARCHAR,COLUMN_NAME)+' AS VARCHAR),''''),'
ELSE 'ISNULL(CAST('+CONVERT(VARCHAR,COLUMN_NAME) +' AS VARCHAR('+CONVERT(VARCHAR,CHARACTER_MAXIMUM_LENGTH)+')),''''),' END
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME IN(@TableName) FOR XML PATH(''))
SELECT @ListXML
Jun 23, 2009
Find PRIMARY KEY column name and table name in sqlsever
select a.Table_Name, a.Column_Name from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE a,
INFORMATION_SCHEMA.TABLE_CONSTRAINTS b
where b.constraint_type='PRIMARY KEY' and a.constraint_name = b.constraint_name
--and a.table_name='T_8KCompany'
order by a.table_name, a.column_name
INFORMATION_SCHEMA.TABLE_CONSTRAINTS b
where b.constraint_type='PRIMARY KEY' and a.constraint_name = b.constraint_name
--and a.table_name='T_8KCompany'
order by a.table_name, a.column_name
Jun 8, 2009
Pivot sample Query in sqlserver
create table #temp
(CustomerID int ,[date] datetime ,
SubTotal varchar(20))
select * from #temp
--insert into #temp values(4,getdate(),34)
SELECT * FROM
(SELECT CustomerID, DATEPART(m, date) OrderMonth, SubTotal
FROM #temp
WHERE date between '20030101' and '20101231'
and CustomerID IN (2,4,6,7,8,9)) src
PIVOT (SUM(SubTotal) FOR OrderMonth
IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) AS pvt
GO
(CustomerID int ,[date] datetime ,
SubTotal varchar(20))
select * from #temp
--insert into #temp values(4,getdate(),34)
SELECT * FROM
(SELECT CustomerID, DATEPART(m, date) OrderMonth, SubTotal
FROM #temp
WHERE date between '20030101' and '20101231'
and CustomerID IN (2,4,6,7,8,9)) src
PIVOT (SUM(SubTotal) FOR OrderMonth
IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) AS pvt
GO
Subscribe to:
Posts (Atom)