sql-server-2005

Get Updated Row

可紊 提交于 2019-12-18 07:19:59
问题 I have a query that updates one record, and only one record. Is there are way to get the Id updated in the same query such as Select ScopeIdentity when inserting. UPDATE Task SET MyTime = GetDate(), MyUserId = @userid FROM (select top 1 table where SomeStuff) Select Lastrow that just got updated. 回答1: Depending on what you are doing, you may need to use the table syntax of OUTPUT . A possibility is to specify a temporary table / table variable. DECLARE @T TABLE ( MyID INT NOT NULL ) UPDATE

SQL Server - Transpose rows into columns

假装没事ソ 提交于 2019-12-18 07:10:31
问题 I've searched high and low for an answer to this so apologies if it's already answered! I have the following result from a query in SQL 2005: ID 1234 1235 1236 1267 1278 What I want is column1|column2|column3|column4|column5 --------------------------------------- 1234 |1235 |1236 |1267 |1278 I can't quite get my head around the pivot operator but this looks like it's going to be involved. I can work with there being only 5 rows for now but a bonus would be for it to be dynamic, i.e. can

Can you do a select on the results of a stored procedure in T-SQL

爷,独闯天下 提交于 2019-12-18 06:23:20
问题 select * from (EXEC sp_SomeStoredProc) If you can't do this then what is stopping it from being added to the SQL standard or T-SQL? 回答1: You can't do this, however you can do it as an insert. e.g. insert mytable exec myStoredProcedure Also, never name your stored procedures sp_xxxx. This is because SQL will always search in the system stored procedure area due to the sp_ before looking in the user stored procedures, leading to a small loss in performance that could add it to be fairly

Creating a SQL table from a xls (Excel) file

一个人想着一个人 提交于 2019-12-18 06:17:09
问题 I'm trying to convert an Excel document into a table in SQL 2005. I found the link below and am wondering if it looks like a solution. If so, what would the @excel_full_file_name syntax be and where would the path be relative to? http://www.siccolo.com/Articles/SQLScripts/how-to-create-sql-to-convert-Excel_to_table.html 回答1: You can use the BULK INSERT T-SQL command if you just want a pure sql solution. You have to save the file as csv/text first. BULK INSERT YourDestinationTable FROM 'D:

Difference between a inline function and a view

随声附和 提交于 2019-12-18 06:16:10
问题 I am a newbie in using functions and it appears to me that an inline function is very similar to a view. Am I correct? Also, can I have UPDATE statements within a function? 回答1: After reading many of the answers here, I'd like to note that there is a big difference between an inline table-valued function and any other kind of function (scalar or multi-line TVF). An inline TVF is simply a parameterized view. It can be expanded and optimized away just like a view. It is not required to

How to force nolock hint for sql server logins

喜夏-厌秋 提交于 2019-12-18 06:09:33
问题 Does anyone know of a way to force a nolock hint on all transactions issued by a certain user? I'd like to provide a login for a support team to query the production system, but I want to protect it by forcing a nolock on everything they do. I'm using SQL Server 2005. 回答1: This is a painful and hacky way to do it, but it's what we're doing where I work. We're also using classic asp so we're using inline sql calls. we actually wrap the sql call in a function (here you can check for a specific

How to force nolock hint for sql server logins

╄→尐↘猪︶ㄣ 提交于 2019-12-18 06:09:31
问题 Does anyone know of a way to force a nolock hint on all transactions issued by a certain user? I'd like to provide a login for a support team to query the production system, but I want to protect it by forcing a nolock on everything they do. I'm using SQL Server 2005. 回答1: This is a painful and hacky way to do it, but it's what we're doing where I work. We're also using classic asp so we're using inline sql calls. we actually wrap the sql call in a function (here you can check for a specific

SQL Server, temporary tables with truncate vs table variable with delete

牧云@^-^@ 提交于 2019-12-18 05:57:18
问题 I have a stored procedure inside which I create a temporary table that typically contains between 1 and 10 rows. This table is truncated and filled many times during the stored procedure. It is truncated as this is faster than delete. Do I get any performance increase by replacing this temporary table with a table variable when I suffer a penalty for using delete (truncate does not work on table variables) Whilst table variables are mainly in memory and are generally faster than temp tables

SQL cast datetime

给你一囗甜甜゛ 提交于 2019-12-18 05:54:43
问题 In SQL Server 2005, why does: PRINT Cast('' AS datetime) display: Jan 1 1900 12:00AM I would have thought it should be null ? 回答1: The empty string is casted to 0 which is later casted to the era date. Unlike Oracle , SQL Server distinguishes between NULL and an empty string. 回答2: It's because empty string '' is not NULL . If you do: select Cast(null AS datetime) OUTPUT: ----------------------- NULL (1 row(s) affected) CAST and CONVERT (Transact-SQL) When character data that represents only

Find the date/time a table's column was created

安稳与你 提交于 2019-12-18 05:53:58
问题 How can you query SQL Server for the creation date for a SQL Server 2005 table column? I tried the sp_columns [tablename] to get that information, but the creation date wasn't included in this stored procedure. How can this be done? 回答1: There's this system table named sys.Columns that you can get columns information from it. if you want to see columns of a particular table you can do as follows: SELECT col.* from sys.objects obj inner join sys.columns col on obj.object_Id=col.object_Id and