tsql

Performance effect of using TOP 1 in a SELECT query

烂漫一生 提交于 2021-02-06 15:00:07
问题 I have a User table where there are a Username and Application columns. Username may repeat but combination of Username + Application is unique, but I don't have the unique constraint set on the table (for performance) Question: will there be any difference (performance-wise) between : SELECT * FROM User where UserName='myuser' AND Application='myapp' AND - SELECT TOP 1 * FROM User where UserName='myuser' AND Application='myapp' As combination of Username + Application is unique, both queries

How to get all id's from inserted table

可紊 提交于 2021-02-05 10:47:50
问题 I am insert bulk of records how to get all those Id's from inserted tables. I want to use all those ids as forgein keys and insert into another table INSERT INTO [dbo].[BudCustomers] ([LegalName] ,[EffectiveDate] ,[LawsonCustomerNumber] ,[ChangeReason] ,[ImportedRecord] ,[VersionID] ,[StatusID] ,[CreatedDate] ,[CreatedUserID] ,[LastModifiedDate] ,[LastModifiedUserID] ,[CustomerGroupID]) SELECT CustomerName ,'1900-01-01 00:00:00.000' , CASE WHEN PATINDEX('%[0-9]%', CustomerName) > 0 THEN

SQL compare two columns for same value

删除回忆录丶 提交于 2021-02-05 10:25:34
问题 With SQL Server 2012, I have two columns that I would like to compare. Both are on the same table, so no joins are needed. Basically I need to compare two columns, for example scan1 and scan2 and if their value's match, then I need a 1 , else 0 . The results of the match would output to AS Results. 回答1: Something like SELECT .... , CASE WHEN scan1 = scan2 THEN 1 ELSE 0 END AS is_equal FROM table1 should do the job. 回答2: You can go like: SELECT CASE WHEN Scan1 = Scan2 THEN 1 ELSE 0 END AS

Dynamic columns depend on previous dynamic columns - TSQL

依然范特西╮ 提交于 2021-02-05 09:33:09
问题 I try to create dynamic forecast for 18(!) months depend on previous columns (months) and i am stuck: I have three columns: Stock SafetyStock Need for production - another select with clause WHERE date = getdate() what i need to achieve: Index , Stock - Current month, SafetyStock -Current month, Need for production (select * from Nfp where date = getdate()), Stock - Current month + 1, Safetystock - Current Month + 1, Need for Production - Current Month + 1 ... etc till 18 months calculations:

Sql where clause to return everything in case filter is null

走远了吗. 提交于 2021-02-05 09:14:05
问题 I have a sql table below - SrNo Name Value 1 A X1 2 B NULL 3 C X3 4 D X4 5 E NULL 6 F NULL I am trying to get the all the records from the table to with below two conditions - a) In case the filter on the '@Value' column is null as below then it should return all the records including null BEGIN Declare @Value varchar(50) SET @Value = NULL SELECT * from TestTable where Value = @Value END b) In case if the @Value is provided which is not null then it should return the column(This surely works)

Determine if record has children in LINQ to SQL

泪湿孤枕 提交于 2021-02-05 08:09:59
问题 I am having at hierarchical table with the structure ID, Name, FK_ID, Sortkey Fetching the data in LINQ to SQL is straight forward: var list = from ls in db.myTable where ls.FK_ID == levelId orderby ls.sortkey ascending select ls; And I can traverse down the tree by linking to the next levelId. But what I can't figure out, if there is a way in LINQ, to check if there is any children I could probably build a view, that added a flag to each record, but I would rather do this in LINQ, if

Determine if record has children in LINQ to SQL

天涯浪子 提交于 2021-02-05 08:09:12
问题 I am having at hierarchical table with the structure ID, Name, FK_ID, Sortkey Fetching the data in LINQ to SQL is straight forward: var list = from ls in db.myTable where ls.FK_ID == levelId orderby ls.sortkey ascending select ls; And I can traverse down the tree by linking to the next levelId. But what I can't figure out, if there is a way in LINQ, to check if there is any children I could probably build a view, that added a flag to each record, but I would rather do this in LINQ, if

T-SQL - Receive the gap between 2 Numbers

会有一股神秘感。 提交于 2021-02-05 08:02:54
问题 I'm using SQL Server 2012. I'm receiving a "Max Number" f.e. 201900005 this tells me the range starts at 201900000 (this is given). Now i want to receive the numbers which are missing in this range. I've looked at several Questions about this, but i can't seem to get it working. Checking the table against itself, using between or using a cursor. Max Number = 201900005, Min Number = 201900000 test_table +----------------+ | test_number | +----------------+ | 201900001 | | 201900003 | |

TSQL make EXECUTE statement synchronous

心已入冬 提交于 2021-02-05 07:39:16
问题 I have two TSQL EXEC statements EXECUTE (N'MyDynamicallyGeneratedStoredProcedure') -- return 0 on success SELECT @errCode = @@ERROR ; IF (@errCode = 0) BEGIN EXEC 'A Sql Statement using ##temptable created from first', @returnValue END How do I make the two EXEC's synchronous? ; Right now the second EXEC does not wait for the first EXECUTE to complete. I tried issuing a WaitFor Delay, It waits but the second EXEC statement is never returing back. Thanks. Update, Here is more info: First

Any way to edit data on MS-Access form using SQL View with two tables

左心房为你撑大大i 提交于 2021-02-05 06:11:26
问题 From what I've read it should be possible to edit data described by a view if the data being modified represents only one table and a unique identifier field is included in the data. According to Microsoft Any modifications must reference columns from only one base table Has anyone had any luck creating an MS-Access form that is editable when the underlying recordset is based on a view or stored procedure merging data from two tables? My form only used the secondary table to join a code table