How to create updateable distributed partitioned view
NEED TO NOTICE:
- SET XACT_ABORT ON : this enables a distributed transaction to auto rollback.
- create check constraint before create primary key
- Better not to use char type, and varchar type. It will cause problem in updateable distributed view, such as not specified the partitioned row. use nchar, or nvarchar instead.
- It seems that the partition can use only "between and", I tried to use %, but does not work, and cause error.
- In my practice, I use foreign key in enterprise manager, instead of t-sql to alter the table. becuase I use t-sql and cause error.
- In JDBC, it seems hard to set the XACT_ABORT on, so instead I used sp_configure sp_configure 'user options', 16384GORECONFIGUREGO to set up the XACT_ABORT on for all users in SQL server.
Here are some examples how to create updatable partitioned view.
- create a table in another computer
USE BookStoreGO
CREATE TABLE [dbo].[books2] ( [bookNo] [bigint] NOT NULL CHECK ([bookNo]BETWEEN 101 AND 200), [bookTitle] [nvarchar](100) NOT NULL, [bookEdition] [smallint] NULL , [bookAuthor] [nvarchar](20) NOT NULL , [bookPrice] [float] NOT NULL , [bookPublisher] [int] NOT NULL , [bookQuantity] [int] NOT NULL
)
ALTER TABLE [books2]ADD PRIMARY KEY ([bookNo])
GO
- create a table in one computer
USE BookStoreGO
CREATE TABLE [dbo].[books1] ( [bookNo] [bigint] NOT NULL CHECK ([bookNo] BETWEEN 1 AND 100), [bookTitle] [nvarchar](100) NOT NULL, [bookEdition] [smallint] NULL , [bookAuthor] [nvarchar](20) NOT NULL , [bookPrice] [float] NOT NULL , [bookPublisher] [int] NOT NULL , [bookQuantity] [int] NOT NULL
)
ALTER TABLE [books1]ADD PRIMARY KEY ([bookNo])
- create view for tables in different tables
CREATE VIEW [dbo].[books_view] ASSELECT * FROM [DESKTOP].[BookStore].[dbo].[books1] UNION ALLSELECT * FROM [DELL].[BookStore].[dbo].[books2]GO
- Add new records to the view.
USE BookStoreGO
SET XACT_ABORT ON
INSERT INTO [books_view]VALUES (4, 'A',1,'B',200.00,1,1)
INSERT INTO [books_view]VALUES (1, 'A',1,'B',200.00,1,1)
- Set the user option for XACT_ABORT on for all users
sp_configure 'user options', 16384GORECONFIGUREGO

0 Comments:
Post a Comment
<< Home