sql 从连接的查询中获取所有列,搜索列名并对其进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 从连接的查询中获取所有列,搜索列名并对其进行排序相关的知识,希望对你有一定的参考价值。


--##############################################################################################################
-- STEP1: PrintBig (StoredProcedure) to print long text 
--##############################################################################################################
/*
-- You can use print big Stored Procedure to print the long query...... (HERE IT IS)
-- https://www.richardswinbank.net/doku.php?id=tsql:print_big
-- http://stackoverflow.com/questions/4833549/nvarcharmax-still-being-truncated
*/
IF OBJECT_ID('tempdb..#usp_PrintBig') IS NOT NULL
  DROP PROCEDURE #usp_PrintBig
GO
CREATE PROCEDURE #usp_PrintBig (
   @text NVARCHAR(MAX)
 )
AS
--DECLARE @text NVARCHAR(MAX) = 'YourTextHere'
DECLARE @lineSep NVARCHAR(2) = CHAR(13) + CHAR(10)  -- Windows \r\n
 
DECLARE @off INT = 1
DECLARE @maxLen INT = 4000
DECLARE @len INT
 
WHILE @off < LEN(@text)
BEGIN
 
  SELECT @len =
    CASE
      WHEN LEN(@text) - @off - 1 <= @maxLen THEN LEN(@text)
      ELSE @maxLen
             - CHARINDEX(REVERSE(@lineSep),  REVERSE(SUBSTRING(@text, @off, @maxLen)))
             - LEN(@lineSep)
             + 1
    END
  PRINT SUBSTRING(@text, @off, @len)
  --PRINT '@off=' + CAST(@off AS VARCHAR) + ' @len=' + CAST(@len AS VARCHAR)
  SET @off += @len + LEN(@lineSep)
END
GO





--############################################################################################################
-- STEP 2:    GET ALL COLLUMN FROM A JOINED QUERY, SEARCH IN COLUMN NAME AND SORT THEM!!
--############################################################################################################

BEGIN TRY
	DECLARE @columnNameLike NVARCHAR(200) = '%%'
	DECLARE @query NVARCHAR(MAX)
	DECLARE @T1 NVARCHAR(200) = 'CustomerInformations'
	DECLARE @T1_JoinColumn NVARCHAR(200) = 'CustomerDynamicInfoId'
	DECLARE @T2 NVARCHAR(200) = 'CustomerDynamicInformations'
	DECLARE @T2_JoinColumn VARCHAR(200) = 'Id'

	SET @query = 'SELECT '+ CHAR(13)
	SELECT @query = @query + TABLE_NAME + '.' + COLUMN_NAME +
	--' AS [' + CASE TABLE_NAME WHEN @T1 THEN COLUMN_NAME+' (T1)' WHEN @T2 THEN COLUMN_NAME+' (T2)' END + ']' +
	',' + CHAR(13) 
	FROM INFORMATION_SCHEMA.COLUMNS 
	WHERE (TABLE_NAME = @T1 OR TABLE_NAME = @T2) 
	AND COLUMN_NAME LIKE @columnNameLike
	ORDER BY COLUMN_NAME

	SET @query =  LEFT(@query, LEN(@query) - 2) + CHAR(13) 
	+ ' FROM ' + @T1 
	+ ' INNER JOIN ' + @T2 
	+ ' ON '+ @T1 + '.' + @T1_JoinColumn + '=' + @T2 + '.' + @T2_JoinColumn

	EXEC(@query)
	EXEC #usp_PrintBig @query 
	--PRINT @query
	/* 
	  -- IF Your table has too many columns, then you might encounter TRUNCATION of query text when printing, 
	  -- then use this Temporary StoredProcedure (#usp_PrintBig) to print long text
	  -- EXEC #usp_PrintBig @query 
	*/
END TRY
BEGIN CATCH
    SELECT 'Column Not Found in either table!' as ErrorMessage
	 ,ERROR_NUMBER() AS ErrorNumber
     ,ERROR_SEVERITY() AS ErrorSeverity
     ,ERROR_STATE() AS ErrorState
     ,ERROR_PROCEDURE() AS ErrorProcedure
     ,ERROR_LINE() AS ErrorLine
     ,ERROR_MESSAGE() AS ErrorDetail;
END CATCH







--=========================== AGAIN Modified for DataType & Length (MoreDetailed, BESTTTTT) =============================
BEGIN TRY
	DECLARE @columnNameLike NVARCHAR(200) = '%Name%'
	DECLARE @query NVARCHAR(MAX)
	DECLARE @T1 NVARCHAR(200) = 'CustomerInformations'
	DECLARE @T1_JoinColumn NVARCHAR(200) = 'CustomerDynamicInfoId'
	DECLARE @T2 NVARCHAR(200) = 'CustomerDynamicInformations'
	DECLARE @T2_JoinColumn VARCHAR(200) = 'Id'

	SET @query = 'SELECT '+ CHAR(13)
	SELECT @query = @query + TABLE_NAME + '.' + COLUMN_NAME +
	' AS [' + COLUMN_NAME + ' @'+
			+ CAST(DATA_TYPE AS NVARCHAR(55))+
			CASE 
			WHEN CHARACTER_OCTET_LENGTH IS NOT NULL 
				THEN '('+ CAST(ISNULL(CHARACTER_OCTET_LENGTH, '') as NVARCHAR(10))+ ')'
			ELSE ''
			END
	+ CASE TABLE_NAME WHEN @T1 THEN ' <T1>' WHEN @T2 THEN ' <T2>' END 
	+ ']' + ',' + CHAR(13) 
	FROM INFORMATION_SCHEMA.COLUMNS 
	WHERE (TABLE_NAME = @T1 OR TABLE_NAME = @T2) 
	AND COLUMN_NAME LIKE @columnNameLike
	ORDER BY COLUMN_NAME

	SET @query =  LEFT(@query, LEN(@query) - 2) + CHAR(13) 
	+ 'FROM ' + @T1 
	+ ' INNER JOIN ' + @T2 
	+ ' ON '+ @T1 + '.' + @T1_JoinColumn + '=' + @T2 + '.' + @T2_JoinColumn

	EXEC(@query)
	EXEC #usp_PrintBig @query 
	--PRINT @query
	/* 
	  -- IF Your table has too many columns, then you might encounter TRUNCATION of query text when printing, 
	  -- then use this Temporary StoredProcedure (#usp_PrintBig) to print long text
	  -- EXEC #usp_PrintBig @query 
	*/
END TRY
BEGIN CATCH
    SELECT 'Column Not Found in either table!' as ErrorMessage
	 ,ERROR_NUMBER() AS ErrorNumber
     ,ERROR_SEVERITY() AS ErrorSeverity
     ,ERROR_STATE() AS ErrorState
     ,ERROR_PROCEDURE() AS ErrorProcedure
     ,ERROR_LINE() AS ErrorLine
     ,ERROR_MESSAGE() AS ErrorDetail;
END CATCH

以上是关于sql 从连接的查询中获取所有列,搜索列名并对其进行排序的主要内容,如果未能解决你的问题,请参考以下文章

SQL查询

在所有表中搜索多个值

将日期附加到字符串以在 oracle 中获取表名并对其进行选择查询

如何从数据库中获取所有表名并对其进行分页

如何从 SqlDataReader 获取列的表名

如何从 SQL Server 中特定数据库的表中获取所有列名?