How do you get the identity value after using MERGE when there is a match?

In the case when the record already exists, you can store the matched id into a variable like this: DECLARE @MatchedId INTEGER; MERGE MyTable as t …. …. WHEN MATCHED THEN UPDATE SET @MatchedId = t.MyTableId; UPDATE: Here’s a full example. This demonstrates one way: DECLARE @UpdateVariable bit DECLARE @ChangeResult TABLE (ChangeType VARCHAR(10), Id INTEGER) … Read more

How to remove white space characters from a string in SQL Server

Using ASCII(RIGHT(ProductAlternateKey, 1)) you can see that the right most character in row 2 is a Line Feed or Ascii Character 10. This can not be removed using the standard LTrim RTrim functions. You could however use (REPLACE(ProductAlternateKey, CHAR(10), ”) You may also want to account for carriage returns and tabs. These three (Line feeds, … Read more

SQL comments on create table on SQL Server 2008

This is what I use /*==============================================================*/ /* Table: TABLE_1 */ /*==============================================================*/ create table TABLE_1 ( ID int identity, COLUMN_1 varchar(10) null, COLUMN_2 varchar(10) null, constraint PK_TABLE_1 primary key nonclustered (ID) ) go declare @CurrentUser sysname select @CurrentUser = user_name() execute sp_addextendedproperty ‘MS_Description’, ‘This is my table comment’, ‘user’, @CurrentUser, ‘table’, ‘TABLE_1’ go declare @CurrentUser sysname … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)