sql-server-2012

What is the advantage of using sp_rename to rename a SQL table?

匆匆过客 提交于 2020-01-04 02:39:05
问题 I have a sql table in sql server 2012 that I need to rename. I know in other database systems a way of executing this is the following: ALTER TABLE table_name RENAME TO new_table_name; However, it seems SQL Server requires different syntax. From SQL Management Studio I renamed the table in the Design View and right clicked to Generate Change Script, and it produced the following: BEGIN TRANSACTION GO EXECUTE sp_rename N'table_name', N'new_table_name', 'OBJECT' GO ALTER TABLE new_table_name

Get Select Column names from dynamic query in SQL Server

怎甘沉沦 提交于 2020-01-03 17:36:47
问题 If I have a random query, and would like to return the column names from this query: SELECT 1 ONE, 2 TWO, 'THREE' THREE How can I return the column names? Maybe even the column data type as well. I can do this in c#, but I am looking for a way to do this all purely inside of Ms Sql. Is this possible, if so how? Would I have to create a temporary table, then check the info schema table? Or can I do it another way? 回答1: You can use sp_describe_first_result_set to describe the columns returned

How to solve Unable to connect to SQL Server Database?

半世苍凉 提交于 2020-01-03 16:52:15
问题 Forgive me if my question is stupid or something as far as I am newbie to the programming. This stack over flow article Visual Studio 2013 and ASP.NET Web Configuration Tool saved me days for running the Visual Studio 2013 WSA Tool but I run into a new problem and it is the connection to SQL Server database. The problem is when I run IIS Express through cmd and then put address in browser I get redirected to WSA Tool but when I hit the security tab or link then I get this error: There is a

Why DROP TABLE doesn't seem to take effect before a SELECT INTO?

倖福魔咒の 提交于 2020-01-03 16:45:39
问题 The following tSQL query is puzzling me: select 1 as FIELD into #TEMP drop table #TEMP select 1 as FIELD into #TEMP When I run it from SQL Server Management Studio session window (pressing F5 to the whole query, as a group), I get the following error: Msg 2714, Level 16, State 1, Line 3 There is already an object named '#TEMP' in the database. Note that table #TEMP doesn't exist before the query is executed. I thought that the code shouldn't produce any errors as line 2 is dropping the

SSIS 2012 date formats dmy vs mdy

半世苍凉 提交于 2020-01-03 08:53:23
问题 There are three SQL Servers: PROD (2008 R2) NEW_TEST (2012) NEW_PROD (2012) I am migrating a large number of SSIS packages from PROD to both NEW_TEST and NEW_PROD servers. Source data comes from flat text files Source DATE data is in the format of dd/mm/yyyy (i.e. 5th of November 2015 is stored as 05/11/2015). In SSIS, the definition of the DATE source column (text file) is Unicode string (DT_WSTR) and the target column (in DB table) data type is DATETIME so a type conversion happens between

SSIS 2012 date formats dmy vs mdy

怎甘沉沦 提交于 2020-01-03 08:53:13
问题 There are three SQL Servers: PROD (2008 R2) NEW_TEST (2012) NEW_PROD (2012) I am migrating a large number of SSIS packages from PROD to both NEW_TEST and NEW_PROD servers. Source data comes from flat text files Source DATE data is in the format of dd/mm/yyyy (i.e. 5th of November 2015 is stored as 05/11/2015). In SSIS, the definition of the DATE source column (text file) is Unicode string (DT_WSTR) and the target column (in DB table) data type is DATETIME so a type conversion happens between

Why can integer be subtracted from DATETIME but not DATE type

被刻印的时光 ゝ 提交于 2020-01-03 08:41:35
问题 Is the relationship between DATETIME & INTEGER and DATE & INTEGER consistent? This executes fine: DECLARE @Yesterday DATETIME = GETDATE(); SELECT @Yesterday-1; As does this: DECLARE @Yesterday DATE = GETDATE(); SELECT @Yesterday; This errors: DECLARE @Yesterday DATE = GETDATE(); SELECT @Yesterday-1; I can safely subtract an integer type from a datetime but not from a date . What is the reason for this behaviour? 回答1: Actually, it is very consistent. DATETIME is a type inherited from previous

Why can integer be subtracted from DATETIME but not DATE type

不羁的心 提交于 2020-01-03 08:41:26
问题 Is the relationship between DATETIME & INTEGER and DATE & INTEGER consistent? This executes fine: DECLARE @Yesterday DATETIME = GETDATE(); SELECT @Yesterday-1; As does this: DECLARE @Yesterday DATE = GETDATE(); SELECT @Yesterday; This errors: DECLARE @Yesterday DATE = GETDATE(); SELECT @Yesterday-1; I can safely subtract an integer type from a datetime but not from a date . What is the reason for this behaviour? 回答1: Actually, it is very consistent. DATETIME is a type inherited from previous

Date format returned as mm/dd/yyyy hh:mm:ss AM/PM

隐身守侯 提交于 2020-01-03 07:37:42
问题 I am brand new to sql my company just kinda threw me head long into this and said do it. So any help is greatly appreciated. I am trying to get a date to come out in the format of mm/dd/yyyy hh:mm:ss AM/PM so for example a date of 09/26/2014 11:04:54 AM. I have tried using the code: Select Convert(nvarchar,EntryDate,101) From DB1 However that returns just 09/26/2014. I also tried Select Convert(nvarchar,EntryDate,100) From DB1 but this returns Sep 26 2014 11:04AM Not sure where to go from

SQL Server Default Database when Query - master

僤鯓⒐⒋嵵緔 提交于 2020-01-03 06:58:04
问题 I've been using SQL Server Management Studio (SSMS) for the last 8 years, and I keep on stumbling upon a problem. When I right click on a Table, and select SELECT TOP 2000 ROWS, the query editor opens up a new file with the query inside. This is nice and all for a quick review of the table. The problem I have is the default database is changed from the actual database to the master database. I have sysadmin rights. The query that gets generated by SSMS, then has the databse, schema, and table