sql-server-2012

Most efficient way to SELECT rows WHERE the ID EXISTS IN a second table

感情迁移 提交于 2019-12-23 13:06:42
问题 I'm looking to select all records from one table where the ID exists in a second table. The following two queries return the correct results: Query 1: SELECT * FROM Table1 t1 WHERE EXISTS (SELECT 1 FROM Table2 t2 WHERE t1.ID = t2.ID) Query 2: SELECT * FROM Table1 t1 WHERE t1.ID IN (SELECT t2.ID FROM Table2 t2) Are one of these queries more efficient than the other? Should I use one over the other? Is there a third method that I didn't think of that is even more efficient? 回答1: Summary: IN and

Read SSIS Package File Name From Within Package (Package Deployment)

馋奶兔 提交于 2019-12-23 13:06:12
问题 I created a SSIS Package in Visual Studio SSDT 2012, using Package Deployment model. Is there any way to get access to the package's file name as a readonly variable or property from within the package? I don't see it listed as a system variable, is there any way maybe through the script task? 回答1: I think you need to check System variables in the name of [System::PackageName], if you want to rename the package name,then you have to change the Properties. I've attached screenshots for your

How expensive is select distinct * query

两盒软妹~` 提交于 2019-12-23 12:35:08
问题 In sql server 2012, I have got a table with more than 25 million rows with duplicates. The table doesn't have unique index. It only has a non-clustered index. I wanted to eliminate duplicates and so, I m thinking of the below select distinct * into #temp_table from primary_table truncate primary_table select * into primary_table from #temp_table I wanted to know how expensive is select distinct * query. If my procedure above is very expensive, I wanted to know if there is another alternate

Sort data before concatenating using STUFF FOR XML

南笙酒味 提交于 2019-12-23 12:16:23
问题 I have the following query that I am using for an SSRS Report: SELECT ROW_NUMBER() OVER ( ORDER BY Judge.EventJudgeID ) AS JudgeRow , Judge.EventID , Judge.Judge_PersonID , STUFF(( SELECT DISTINCT ',' + CAST(Fights.FightNumber AS VARCHAR(MAX)) AS [text()] FROM dbo.tblFights Fights , dbo.tblFightJudge FRJudge WHERE Fights.FightID = FRJudge.fightid AND ( Judge.Judge_PersonID = FRJudge.judge1id OR Judge.Judge_PersonID = FRJudge.judge2id OR Judge.Judge_PersonID = FRJudge.judge3id ) FOR XML PATH('

Missing Microsoft Excel as option in SQL Server data import tool

为君一笑 提交于 2019-12-23 12:15:55
问题 I'm running SQL Server 2012 on a 64bit Windows Server 2012 R2. I don't have MS Office installed. I now notice that when I start Start->Program Files->Microsoft SQL Server 2012->Import and Export Data (64-bit) the MS Excel file option is missing from the datasource dropdown. How can I get that option without having to install Excel or Office on my server? 回答1: Had to install this driver: https://www.microsoft.com/en-us/download/confirmation.aspx?id=13255 After that it works :) 来源: https:/

DMF sys.dm_exec_sql_text not showing DBID

限于喜欢 提交于 2019-12-23 12:11:15
问题 I have got this query from http://technet.microsoft.com/en-us/library/ms181929.aspx SELECT s2.dbid, s1.sql_handle, (SELECT TOP 1 SUBSTRING(s2.text,statement_start_offset / 2+1 , ( (CASE WHEN statement_end_offset = -1 THEN (LEN(CONVERT(nvarchar(max),s2.text)) * 2) ELSE statement_end_offset END) - statement_start_offset) / 2+1)) AS sql_statement, execution_count, plan_generation_num, last_execution_time, total_worker_time, last_worker_time, min_worker_time, max_worker_time, total_physical_reads

How to add datetime field with a time field

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 11:41:13
问题 I have to add time value to existing datetime value using T-SQL in SQL Server 2012. I was thinking that DATEADD function it might be a solution, but it is not... Perhaps I have somehow time convert to datetime ? So I have StartDate 2013-02-18 18:34:40.330 (datetime) Interval 00:11:00.0000000 (time) EndDate ? tsql ? (datetime) Any clue? 回答1: Try something like this. Note: I am not taking milliseconds here declare @dt datetime = getdate() declare @t time = '01:35:45' select dateadd(second,

How to add datetime field with a time field

ぐ巨炮叔叔 提交于 2019-12-23 11:40:53
问题 I have to add time value to existing datetime value using T-SQL in SQL Server 2012. I was thinking that DATEADD function it might be a solution, but it is not... Perhaps I have somehow time convert to datetime ? So I have StartDate 2013-02-18 18:34:40.330 (datetime) Interval 00:11:00.0000000 (time) EndDate ? tsql ? (datetime) Any clue? 回答1: Try something like this. Note: I am not taking milliseconds here declare @dt datetime = getdate() declare @t time = '01:35:45' select dateadd(second,

TSQL Select from Different Table based on a CASE value

空扰寡人 提交于 2019-12-23 09:47:24
问题 I'm running on SQL Server 2012. Imagine I have 3 tables (Table1, Table2, Table3) that all have in common 2 fields (ID, WhereParam). What would be the right way to achieve this? SELECT ID FROM (CASE @Var WHEN "AA" THEN Table1 WHEN "BB" THEN Table2 ELSE Table3) WHERE "TableSelected".WhereParam = @WhereParam 回答1: Using IF (the proper way) IF @Var = 'AA' BEGIN SELECT ID FROM Table1 -- WHERE ... END ELSE IF @Var = 'BB' BEGIN SELECT ID FROM Table2 -- WHERE ... END ELSE BEGIN SELECT ID FROM Table3 -

SQL Server: IF EXISTS massively slowing down a query

為{幸葍}努か 提交于 2019-12-23 09:27:59
问题 (SQL Server 2012 being used) I found some topics on query optimization, and comparing EXISTS to COUNT, but I couldn't find this exact problem. I have a query that looks something like this: select * from tblAccount as acc join tblUser as user on acc.AccountId = user.AccountId join tblAddress as addr on acc.AccountId = addr.AccountId ... **a few more joins** where acc.AccountId in ( select * accountid from (select accountid, count(*) from tblUser where flag = 1 group by accountId) as tbl where