将JSON表转换为XML表(SQL Server)

时间:2015-12-30 06:49:54

标签: sql sql-server json xml sql-server-2014

如果nvarchar(max)更新或插入TableJSON,如何将所有 JSON列[TableXML]从TableJSON转换为nvarchar(max)中的XML列(在触发器中)?

获取所有DECLARE @COLUMN_NAME nvarchar(max) SELECT @COLUMN_NAME = COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TableJSON' AND DATA_TYPE LIKE 'nvarchar' AND CHARACTER_MAXIMUM_LENGTH = -1 --NVARCHAR(MAX) 列:

   ID        bigint,
   Code      char(10),
   Field1    nvarchar(max),
   ...
   Field100  nvarchar(max)

将JSON转换为XML: https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-server/

TableJSON:

   ID        bigint,
   Code      char(10),
   Field1    xml,
   ...
   Field100  xml

TableXML:

Field1

我的触发器(目前仅适用于SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER TRIGGER [dbo].[UpdateXMLTable] ON [dbo].[TableJSON] AFTER INSERT, UPDATE AS BEGIN DECLARE @ID bigint DECLARE @Field1 nvarchar(max) DECLARE @MyHierarchy Hierarchy,@Field1XML XML SELECT @ID = ID, @Field1 = Field1 FROM INSERTED INSERT INTO @myHierarchy SELECT * FROM parseJSON(@Field1) SELECT dbo.ToXML(@MyHierarchy) SET @Field1XML = dbo.ToXML(@MyHierarchy) IF NOT EXISTS(SELECT 1 FROM [dbo].[TableXML] WHERE ID = @ID) INSERT INTO [dbo].[TableXML] (ID, Field1) VALUES (@ID, @Field1XML) ELSE IF (UPDATE (Field1)) UPDATE [dbo].[TableXML] SET Field1 = @Field1XML WHERE ID = @ID END ):

TableJSON

如何将TableXML中的所有 JSON列从public function clock_in() { $this->load->model('model_users'); $this->db->where('USER_EMAIL', $this->session->userdata('USER_EMAIL')); $query = $this->db->get('clocked_in_users'); if($query->num_rows() == 1){ echo "Already Clocked in"; exit; }else{ $this->model_users->clock_user_in(); }; redirect('main/members'); } 转换为XML列?

0 个答案:

没有答案