Home > Database > Mysql Tutorial > body text

获得XML格式的SQLServer图表结构

WBOY
Release: 2016-06-07 15:03:54
Original
1364 people have browsed it

程序员经常问我在sql server (WINDOWS平台上强大的数据库平台) 2000中是否有内置的存储过程, 将图表结构以xml (标准化越来越近了) 格式返回。 如果前端或中间层的代码将数据转给xml (标准化越来越近了) 格式的存储过程。类似下面的图表结构将会比较有用: o


  程序员经常问我在sql server(WINDOWS平台上强大的数据库平台) 2000中是否有内置的存储过程, 将图表结构以xml(标准化越来越近了)格式返回。
  如果前端或中间层的代码将数据转给xml(标准化越来越近了)格式的存储过程。类似下面的图表结构将会比较有用:
  
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  

  
  一旦程序员拥有图表的xml(标准化越来越近了)框架, 他们就可定义传到存储过程的xml(标准化越来越近了)字符串的属性和元素的同样名称。这样通过OPENxml(标准化越来越近了)函数来编写存储过程就很简单了---你不需要再要了解前端xml(标准化越来越近了)的属性和元素如何被映射到特定图表的列名了。
  
  不幸的是, 没有内置的存储过程可以返还图表的xml(标准化越来越近了)框架。但开发一个不难。特定图表中的列名可以通过查寻syscolumns系统图表,information_schema.columns的view,或执行sp_columns系统存储过程获得。这篇文章中我用的是information_schema.columns, 因为微软推荐information_schema view而不是系统图表。注意存储过程只接受两个参数:图表名和拥有者名称, 这是必要的。 因为在一个数据库中多个用户可以拥有同样名称的图表。如果你不指定数据库的拥有者, 存储过程将会假设你需要建立一个数据库本身拥有的图表。
  
  CREATE PROCEDURE dbo.get_table_xml(标准化越来越近了)_structure (
   @table_name VARCHAR(255),
   @owner_name VARCHAR(30) = 'dbo'
  )
  AS
  SET NOCOUNT ON
  /* table variable to hold values */
  DECLARE @temp TABLE (
   string_value VARCHAR(4000)
   )
  
  /* check if the table exists */
  
  IF NOT EXISTS (
   SELECT a.name
   FROM sysobjects a INNER JOIN sysusers b
   ON a.uid = b.uid
   AND a.type = 'u'
   AND a.name = @table_name
   AND b.name = @owner_name)
  
  BEGIN
   RAISERROR('incorrect table name specified, try again', 16,
  1)
   RETURN
  END
  
  INSERT @temp
  SELECT ''
  
  /* append a few blank spaces to make the output readable */
  INSERT @temp
  SELECT '   ' + '   + '/>'
  FROM information_schema.columns
  WHERE
   table_name = @table_name
  AND
   table_schema = @owner_name
  ORDER BY ordinal_position
  
  INSERT @temp
  SELECT '
  
  一旦存储过程生成, 你可以按以下方式执行:
  EXEC get_table_xml(标准化越来越近了)_structure 'orders', 'dbo'
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!