简体   繁体   中英

Table Merge with sql server 2008

I have a database with a usertable. I want to update all users with a csv file. I imported my csv file into a temporary table with SqlBulkCopy.

My next step would be to create a stored procedure that updates my users table using the temporary table. So far i have this:

USE [Userdata]
GO
/****** Object:  StoredProcedure [dbo].[aml_UserImport]    Script Date: 03/06/2013 14:11:44 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
-- =============================================
-- Author:      michiel reyers
-- Create date: 07-01-2013
-- Description: 
-- =============================================
CREATE PROCEDURE [dbo].[aml_UserImport2]

AS

    SET NOCOUNT ON;

    MERGE users
        USING temp_users ON temp_users.ID = users.ID
        WHEN MATCHED 
            THEN UPDATE SET users.typeId = temp_users.typeId,
                                users.Name = temp_users.Name,
                                users.active = 1
        WHEN NOT MATCHED BY SOURCE
            THEN UPDATE SET users.active = 0
        WHEN NOT MATCHED BY TARGET
            THEN Insert (ID,typeID,Name,active) 
            VALUES (temp_users.ID,temp_users.typeId,temp_users.Name,1)
    ;

When i try to execute this i get the error:

Msg 102, Level 15, State 1, Procedure aml_UserImport2, Line 13 Incorrect syntax near 'MERGE'.

What am i doing wrong?

I added the semicolumn, but still no luck :-(

UPDATE:

I did a recheck, and altough I am using MS SQL Server Management 2008 R2. It seems that my database has the compatability level SQL Server 2005 (90). That probably explains why this is not working....

Thanks everyone!

Put a ; after merge.

The MERGE statement requires a semicolon (;) as a statement terminator. Error 10713 is raised when a MERGE statement is run without the terminator.(from MSDN)

My suggestions are:

  1. Add a semicolon at the end of the merge expression
  2. Please check if ACTIVE field =defined as INT. in case it is TINYINT please cast the 1 explicetly

In Merge statement with SQL server you need to end it with ; Semi-colon

Check Syntax here MSDN

MERGE users
    USING temp_users ON temp_users.ID = users.ID
    WHEN MATCHED 
        THEN UPDATE SET users.typeId = temp_users.typeId,
                        users.Name = temp_users.Name,
                        users.active = 1
    WHEN NOT MATCHED BY SOURCE
        THEN UPDATE SET users.active = 0
    WHEN NOT MATCHED BY TARGET
        THEN Insert (ID,typeID,Name,active) 
            VALUES (temp_users.ID,temp_users.typeId,temp_users.Name,1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM