sql-server-2012

SQL Script to clone database leaving original untouched

心不动则不痛 提交于 2019-12-13 15:43:17
问题 we currently have a base installation of our CMS, in this CMS it contains a complete working dataset for users, products, content etc. We are looking to increase our installation time because right now we currently have to go into SQL Server 2012, create a new DB and then restore the DB from an existing base installation db. This can take up to 10 - 15 minutes each installation we do. We also make sure our base database has all the requirements for sites we build. Our issue is, we would like

AnyDac aka FireDac cannot generate update query

孤街醉人 提交于 2019-12-13 15:13:35
问题 I was using UniDac for a long time now and decided to move on to FireDac as it has good Asynch methods after moving on i saw that none of my data editing works anymore it gives me an error: [FireDAC][Phys]-330. Cannot generate update query. Update table undefined. What I am trying to do here is i have a TFDStoredProc component who gets all the data from the database and lets me edit it, with unidac I could easily edit the data without any problem like this: StoredProc.Edit;

SQL Server : Decimal Precision/Scale are yielding strange results

给你一囗甜甜゛ 提交于 2019-12-13 14:23:52
问题 I was working on a bit of SQL for a project, and I noticed some seemingly strange behavior in SQL Server, with regard to what the answer looks like when dividing with decimals. Here are some examples which illustrate the behavior I'm seeing: DECLARE @Ratio Decimal(38,16) SET @Ratio = CAST(210 as Decimal(38,16))/CAST(222 as Decimal(38,16)); select @Ratio -- Results in 0.9459450000000000 DECLARE @Ratio Decimal(38,16) SET @Ratio = CAST(210 as Decimal)/CAST(222 as Decimal); select @Ratio --

How to select every Monday date and every Friday date in the year

一曲冷凌霜 提交于 2019-12-13 14:10:09
问题 basically I want to be able to select monday and friday for every week in the year. So for example this week coming i want 9/29/2014 and 10/3/2014, but i want this for every week in the year. 回答1: Here's one way (you might need to check which day of the week is setup to be the first, here I have Sunday as the first day of the week) You can use a table with many rows (more than 365) to CROSS JOIN to in order to get a run of dates (a tally table). My sys columns has over 800 rows in, you could

How does sys.dm_exec_sql_text work?

社会主义新天地 提交于 2019-12-13 13:45:36
问题 Can you please explain why select * from sys.dm_exec_sql_text (sql_handle) throws an error (Invalid column name 'sql_handle'), but select * from sys.sysprocesses cross apply sys.dm_exec_sql_text (sql_handle) is a valid query? Thank you. 回答1: You need to join it to another table to get the sql_handle (or plan_handle). For example: select a.session_id, a.start_time, status, a.command, text from sys.dm_exec_requests a cross apply sys.dm_exec_sql_text(sql_handle). sys.dm_exec_sql_text is a table

How to calculate percentage of each row in SQL Server 2012?

不问归期 提交于 2019-12-13 13:26:46
问题 I have a table in SQL Server 2012 with data like this: Request_Type Total_Count --------------------------- General 25 Inquiry 15 I want to return the percentage of counts in the last column like this: Request_Type Total_Count Total_Percentage -------------------------------------------------- General 25 62.5 Inquiry 15 37.5 I tried the following query, select Request_Type, Total_Count, (Total_Count* 100) / isnull(sum(Total_Count),0) as Total_Percentage from tbl_Request but it returns 100 in

Running DTS packages on SQL Server 2012

偶尔善良 提交于 2019-12-13 13:02:36
问题 I read that DTS packages aren't supported in SQL Server 2012. I know there's a Backward Compatibility package/option out there that we used for SQL Server 2008R2 to run DTS packages. Will that Backwards compatibility package not work for SQL Server 2012? For 2008 R2 http://msdn.microsoft.com/en-us/library/bb500440(v=sql.105).aspx 回答1: As the documentation says, support for "migrating or running" DTS packages was completely discontinued in SQL Server 2012. Even if you convince your customers

Resetting Primary key without deleting truncating table

浪尽此生 提交于 2019-12-13 12:29:30
问题 I have a table with a primary key, now without any reason I don't know when I am inserting data it is being loaded like this Pk_Col Some_Other_Col 1 A 2 B 3 C 1002 D 1003 E 1901 F Is there any way I can reset my table like below, without deleting/ truncating the table? Pk_Col Some_Other_Col 1 A 2 B 3 C 4 D 5 E 6 F 回答1: You can't update the IDENTITY column so DELETE/INSERT is the only way. You can reseed the IDENTITY column and recreate the data, like this: DBCC CHECKIDENT ('dbo.tbl',RESEED,0)

Use SELECT result as COLUMN name in other SELECT

喜夏-厌秋 提交于 2019-12-13 12:04:27
问题 Is it possible to use the result of a select as a string to concatenate with another string in column name in other select? Example: SELECT brand FROM articles a WHERE a.id='12345678' Result: BRAND_A I now want to concatenate _PRICE to BRAND_A ... SELECT ( SELECT brand FROM articles a WHERE a.id = '12345678' ) + "_PRICE" FROM prices p WHERE p.id = '12345678' ...to actually retrieve: SELECT BRAND_A_PRICE FROM prices p WHERE p.id = '12345678' 回答1: You don't need dynamic SQL to do this (and

How to select two products where it is in same OrderDetails

给你一囗甜甜゛ 提交于 2019-12-13 10:23:46
问题 How do I select an order with two products in the OrderDetails ? For example, if I have 4 orders: order id: 11000 contains: p1, p3, p5, p9 order id: 12000 contains: p1, p4, p5, p8 order id: 13000 contains: p2, p3, p5, p7 order id: 14000 contains: p1, p3, p5, p8 order id: 15000 contains: p2, p3, p6, p9 I want to select order ids where they contain p1 and p9 The expected result: 11000, 12000, 14000 How do I do this in SQL Server? 回答1: You can use something like: SELECT id FROM OrderDetails