tsql

How to return T-SQL query with column names as first row

一世执手 提交于 2020-04-13 07:32:20
问题 I'm writing a SSIS package to output data from a SQL Server 2012 database to a .CSV file for a client. The requirement is that the first row be the column names. Below is the query I've written for the Source in the Data Flow Task. The problem is, it always returns the column names as the LAST row, not the first. Why? How do I achieve this? DECLARE @Today AS DateTime= GETDATE() DECLARE @NextPayrollDate AS DateTime EXEC mobile.getNextPayrollDate @Today, @NextPayrollDate OUTPUT ;WITH

Query with a UNION sub-query takes a very long time

落花浮王杯 提交于 2020-04-13 07:04:11
问题 I've been having an odd problem with some queries that depend on a sub query. They run lightning fast, until I use a UNION statement in the sub query. Then they run endlessly, I've given after 10 minutes. The scenario I'm describing now isn't the original one I started with, but I think it cuts out a lot of possible problems yet yields the same problem. So even though it's a pointless query, bear with me! I have a table: tblUser - 100,000 rows tblFavourites - 200,000 rows If I execute: SELECT

SQL Server - Round TIME values to the next minute

假装没事ソ 提交于 2020-04-13 05:54:10
问题 I've found many posts about rounding "down" time values (e.g. https://stackoverflow.com/a/6667041/468823), but I have another problem: I wanna round to the higher minute and not to the lower, how can I do? My code: SELECT PA.ORE AS TOT_HOURS, CAST(CAST(PA.ORA_INIZIO AS DATETIME) AS TIME) AS BEGIN_TIME, CAST(dateadd(minute, datediff(minute, 0, (CAST(PA.ORA_INIZIO AS DATETIME))), 0) AS TIME) AS BEGIN_TIME_ROUNDED FROM PRG_ATTIVITA PA INNER JOIN PRG_TIPI_ATTIVITA PTA ON PA.ID_TIPO_ATTIVITA = PTA

How to use ROW_NUMBER() in UPDATE clause? [duplicate]

笑着哭i 提交于 2020-04-10 06:47:05
问题 This question already has answers here : SQL Update with row_number() (8 answers) Closed 4 years ago . ROW_NUMBER() is only for used in the SELECT clause in MS SQL Server , but I want to use it for update like the following: Update MyTab Set MyNo = 123 + ROW_NUMBER() over (Order By ID) Where a=b; then I got Error like, Windowed functions can only appear in the SELECT or ORDER BY clauses. How to use ROW_NUMBER() in UPDATE clause? 回答1: DECLARE @MyTable TABLE ( ID INT IDENTITY(2,2) PRIMARY KEY,

Get structure of temp table (like generate sql script) and clear temp table for current instance

大兔子大兔子 提交于 2020-04-07 11:10:26
问题 How do I get structure of temp table then delete temp table. Is there a sp_helptext for temp tables? Finally is it possible to then delete temp table in same session or query window? Example: select * into #myTempTable -- creates a new temp table from tMyTable -- some table in your database tempdb..sp_help #myTempTable Reference. 回答1: You need to use quotes around the temp table name and you can delete the temp table directly after using drop table ... . select * into #myTempTable -- creates

MS SQL Server View not allowing CONCAT

非 Y 不嫁゛ 提交于 2020-04-07 08:00:06
问题 MS SQL Server 2014. I have a SQL statement that works fine: SELECT CONCAT ( CAST(T1.[F1] AS INTEGER), CAST(T1.[F2] AS INTEGER) ) AS F3 FROM mytable AS T1 If I then put this into a view, and try to run I receive the error: Operand data type int is invalid for concat operator F1 and F2 both contain decimals but I want them concatenating e.g.: F1 = 123.00000 F2 = 456.00000 Therefore F3 = 123456 Why does the view not allow this and is there a solution? 回答1: Don't use the visual designers. They

Dynamic Schema name in SQL View

房东的猫 提交于 2020-04-07 06:18:39
问题 I have two datasets: one is data about dogs [my data] the second is a lookup table of matching keys [I have no control over this data] The matching keys are updated regularly, and I want to create a View (or something that fulfills the same purpose) of the Dog dataset, which always joins on the most recent matching keys. Furthermore, I need to be able to reference it inline - as though it was a table . The match updates in the lookup table are differentiated by their schema names, so to get

Dynamic Schema name in SQL View

那年仲夏 提交于 2020-04-07 06:18:30
问题 I have two datasets: one is data about dogs [my data] the second is a lookup table of matching keys [I have no control over this data] The matching keys are updated regularly, and I want to create a View (or something that fulfills the same purpose) of the Dog dataset, which always joins on the most recent matching keys. Furthermore, I need to be able to reference it inline - as though it was a table . The match updates in the lookup table are differentiated by their schema names, so to get

How to pivot without knowing fixed columns in T-SQL

本小妞迷上赌 提交于 2020-04-05 05:13:22
问题 I have a table called balance which I wish to pivot, however it is quite difficult since the column names would be labelled 1,2,3 and balances would be sorted by descending order per customer. This is screenshot of current table: And the pivot table: I reviewed other videos and post but I didn't find a solution to match my current situation what I want to achieve. So the final results would be the customer would be sorted by asc and balances would be sorted by desc. So for customer 3 the

Need T-SQL help for converting Rows from multiple tables into Columns with values in Sql Server 2005

半世苍凉 提交于 2020-03-26 17:00:20
问题 I have a table tblUser: tblUser and a table tblBCE and data in the given below: tblBCE and a master table mstBCE which contains label text to display in front end application. This table contains data for other tab also but currently i want only for tabType 'BCE',I will do for other tab myself once i got the concept. mstBCE There is no relation between tblBCE and mstBCE tables,we need to follow the top to bottom sequence only. In front end application i display these data only by sequence i.e