sql-server-2008-r2

Merge insert with select statement

試著忘記壹切 提交于 2019-12-08 16:53:49
问题 This works for me MERGE Table1 AS tgt USING ( SELECT TOP 1 * FROM Table2, ( SELECT itmid FROM Table3 WHERE id = @id ) as a WHERE id = @id ) AS src ON ( tgt.id = src.id ) WHEN MATCHED THEN UPDATE SET qty = qty + @qty WHEN NOT MATCHED THEN INSERT itmid VALUES itmid; But when i change like this, its not working, showing error near last select MERGE Table1 AS tgt USING ( SELECT TOP 1 * FROM Table2 WHERE id = @id ) AS src ON ( tgt.id = src.id ) WHEN MATCHED THEN UPDATE SET qty = qty + @qty WHEN

Soft Delete - Use IsDeleted flag or separate joiner table?

╄→гoц情女王★ 提交于 2019-12-08 16:29:49
问题 Should we use a flag for soft deletes, or a separate joiner table? Which is more efficient? Database is SQL Server. Background Information A while back we had a DB consultant come in and look at our database schema. When we soft delete a record, we would update an IsDeleted flag on the appropriate table(s). It was suggested that instead of using a flag, store the deleted records in a seperate table and use a join as that would be better. I've put that suggestion to the test, but at least on

sp giving output but SqlDataReader not

女生的网名这么多〃 提交于 2019-12-08 11:59:59
问题 I am using SP in C# project to retrieve the output using SqlDataReader. Below is the code. public List<LMTUsage> GetCompanyID(string userID, int roleId, String Organisation, String BusinessArea) { List<LMTUsage> objLMT = new List<LMTUsage>(); LMTUsage _oELMTUsage; SqlConnection oCon = new SqlConnection(ConfigurationManager.ConnectionStrings["LMTConnectionString"].ConnectionString); oCon.Open(); try { using (SqlCommand _oCmd = new SqlCommand()) { _oCmd.Connection = oCon; _oCmd.CommandType =

How can I continue execution of the current block after an INSERT failure?

☆樱花仙子☆ 提交于 2019-12-08 11:56:08
问题 Consider the following table: CREATE TABLE [dbo].[OrderingTest]( [ColKey] [int] IDENTITY(1,1) NOT NULL, [Col0] [int] NULL, [Col1] [int] NOT NULL, [Col2] [int] NOT NULL ) ON [PRIMARY] ... and the following batch: BEGIN INSERT INTO OrderingTest(Col0,Col1,Col2) VALUES (1,NULL,3); SELECT SCOPE_IDENTITY(); END; The Test table does not allow NULL values in Col1, so the insert will fail. I was hoping to test for failure using the value of SCOPE_IDENTITY() at this point. However, instead of seeing

SQL Server 2008 R2: Pattern matching string vice versa

时光怂恿深爱的人放手 提交于 2019-12-08 11:48:45
问题 I have the following table: Table: CREATE TABLE str_matching ( colstr varchar(200) ); Insert data: INSERT INTO str_matching VALUES('5sXYZA1010B') INSERT INTO str_matching VALUES('A1010B') INSERT INTO str_matching VALUES('AMZ103B15K') INSERT INTO str_matching VALUES('B15K') INSERT INTO str_matching VALUES('XC101') INSERT INTO str_matching VALUES('C101') INSERT INTO str_matching VALUES('502KMD1FZ10009L') INSERT INTO str_matching VALUES('FZ10009L') INSERT INTO str_matching VALUES('A9L') INSERT

eCommerce items management

寵の児 提交于 2019-12-08 11:29:43
问题 I'm creating an app that selling tickets, I have a table for available tickets id | serial | sold ------------------------------- 1 | 000001 | false 2 | 000002 | false 3 | 000003 | true When a user request a ticket the app select a ticket and mark it as sold. My question is: how to manage this table and prevent selling the same ticket to a different user knowing that there will be a hundreds of requests at the same time? I'm thinking about locking but when the row is locked for a transaction,

How to process an integer list row by row sent to a procedure in SQL Server 2008 R2?

∥☆過路亽.° 提交于 2019-12-08 10:14:11
问题 I have an ArrayList (in C#) that contains some int numbers (those are IDs in a table), I want to select some data for each number(s) in this ArrayList and return a table variable or a #temporary table :) I found a solution for passing this ArrayList as an user-defined table type to my stored procedure: CREATE TYPE [dbo].[integer_list_tbltype] AS TABLE( [n] [int] NOT NULL, PRIMARY KEY CLUSTERED ([n] ASC) WITH (IGNORE_DUP_KEY = OFF) ) GO CREATE PROCEDURE [dbo].[Sp_apr_get_apraisors] (

remove null values and merge sql server 2008 r2

a 夏天 提交于 2019-12-08 09:41:30
问题 I have a table (TestTable) as follows PK | COL1 | COL2 | COL3 1 | 3 | NULL | NULL 2 | 3 | 43 | 1.5 3 | 4 | NULL | NULL 4 | 4 | NULL | NULL 5 | 4 | 48 | 10.5 6 | NULL | NULL | NULL 7 | NULL | NULL | NULL 8 | NULL | NULL | NULL 9 | 5 | NULL | NULL 10 | 5 | NULL | NULL 11 | 5 | 55 | 95 I would like a result as follows PK | COL1 | COL2 | COL3 1 | 3 | 43 | 1.5 2 | 4 | 48 | 10.5 3 | 5 | 55 | 95 回答1: You can do this, But it won't give you a serial number for the PK : SELECT PK, MAX(Col1) AS Col1,

BCP IN Error = [Microsoft][SQL Server Native Client 10.0]Invalid date format

孤人 提交于 2019-12-08 08:47:31
I am trying to insert some values in a table through BCP-IN by executing batch file.But facing this issue- Starting copy... SQLState = 22008, NativeError = 0 Error = [Microsoft][SQL Server Native Client 10.0]Invalid date format SQLState = 22008, NativeError = 0 Error = [Microsoft][SQL Server Native Client 10.0]Invalid date format SQLState = 22008, NativeError = 0 Error = [Microsoft][SQL Server Native Client 10.0]Invalid date format SQLState = 22005, NativeError = 0 The file thorugh which data getting inserted is like this- Marcus, Tom 371332 11-1-09 0:00 720.04 25.2 108.01 0 43.2 And the table

preventing a specific record from being deleted

随声附和 提交于 2019-12-08 08:43:07
问题 I want to prevent a specific record from being deleted. This trigger works fine for that specific record. However, other records still remain when they're being deleted. Why? ALTER TRIGGER [Globalization].[CountriesTracker] ON [Globalization].[Countries] INSTEAD OF DELETE AS BEGIN SET NOCOUNT ON; IF ((Select COUNT(*) from [Deleted] Where [Deleted].[CountryId] = '36bd1536-fb56-4ec4-957e-1b3afde16c56') = 1) BEGIN RAISERROR('You can not delete this specific record!', 0, 0) ROLLBACK TRANSACTION