SQL bit-by-bit filtering data column information

Source: Internet
Author: User

In the previous two slides, I shared with us the role of SET QUOTED_IDENTIFIER and introduced a simple string segmentation function. This time, I will introduce how to find out the types, field sizes, whether it can be blank, whether it is a primary key, constraints, and other information of all data columns from SQL Server.

The project needs to import data from the Access database to SQL Server. It needs to check the integrity of the imported data and whether the data value is correct. We use Microsoft SQL Server 2008 Migration Assistant for Access, which is used to export the database in Access to SQL Server, our doubt is whether the export process will result in data errors or lost associations between data due to the reasons we think. It seems that there are a few attempts, but we still need to find a method for testing. As a result, today's problem arises: how to find out the types, field sizes, whether it can be blank, whether it is a primary key, constraints, and other information of all data columns from SQL Server. I am looking for a lot of information to drum up this stored procedure. Let's take a look at the Code:

 
  1. USE [MIS]  
  2. GO  
  3.  
  4. /****** Object:  StoredProcedure [dbo].[sp_SelectColumnInfor]    Script Date: 09/23/2010 19:00:28 ******/  
  5. SET ANSI_NULLS ON 
  6. GO  
  7.  
  8. SET QUOTED_IDENTIFIER ON 
  9. GO  
  10.  
  11. create procedure [dbo].[sp_SelectColumnInfor]  
  12. as 
  13. declare @table_name varchar(250)  
  14. --create a temp table  
  15. create table #tempTable(  
  16. TABLE_NAME nvarchar(128),  
  17. COLUMN_NAME nvarchar(128),  
  18. IS_NULLABLE varchar(3),  
  19. DATA_TYPE nvarchar(128),  
  20. CHARACTER_MAXIMUM_LENGTH int,  
  21. CONSTRAINT_NAME nvarchar(128),  
  22. )   
  23. --create a cursor  
  24. declare curTABLE cursor for 
  25. select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_TYPE='BASE TABLE' 
  26. for read only 
  27.  
  28. open curTABLE  
  29. fetch next from curTABLE into @table_name  
  30. while @@FETCH_STATUS =0  
  31. begin 
  32. insert into #tempTable  
  33. select sc.[TABLE_NAME],sc.[COLUMN_NAME],sc.[IS_NULLABLE],sc.[DATA_TYPE],sc.[CHARACTER_MAXIMUM_LENGTH]  
  34. ,scc.CONSTRAINT_NAME  
  35. from INFORMATION_SCHEMA.COLUMNS sc   
  36. left join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE scc on sc.COLUMN_NAME=scc.COLUMN_NAME and sc.TABLE_NAME=scc.TABLE_NAME  
  37. where sc.[TABLE_NAME]=@table_name --order by TABLE_NAME,COLUMN_NAME  
  38.  
  39. fetch next from curTABLE into @table_name  
  40. end 
  41. close curTABLE  
  42. deallocate curTABLE  
  43. select * from #tempTable order by TABLE_NAME,COLUMN_NAME  
  44. drop table #tempTable  
  45. GO 


In fact, it is very easy to check the functions of the INFORMATION_SCHEMA.COLUMNS and INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE system views. To see the results of executing this stored procedure:

 

Next, we will introduce Microsoft SQL Server 2008 Migration Assistant for Access.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.