sql-server-2008-r2

Get Root parent of child in Hierarchical table

老子叫甜甜 提交于 2020-01-23 07:54:18
问题 I have a table with hierarchical data in it, the structure goes like this: ID ParentId ---- ---------- 1 NULL 2 1 3 2 4 2 5 3 6 5 If I pass the node Id I would like to get the top most node Id/details by traversing through all its parents in SQL. I tried CTE, i somehow cannot get the combination correct. However, i got this working as a function but it is so slow that i had to post this question. In the above example if I pass 6, i would want to have the top most i.e. 1. By traversing through

Get Root parent of child in Hierarchical table

帅比萌擦擦* 提交于 2020-01-23 07:51:27
问题 I have a table with hierarchical data in it, the structure goes like this: ID ParentId ---- ---------- 1 NULL 2 1 3 2 4 2 5 3 6 5 If I pass the node Id I would like to get the top most node Id/details by traversing through all its parents in SQL. I tried CTE, i somehow cannot get the combination correct. However, i got this working as a function but it is so slow that i had to post this question. In the above example if I pass 6, i would want to have the top most i.e. 1. By traversing through

OptimisticConcurrencyException — SQL 2008 R2 Instead of Insert Trigger with Entity Framework

半腔热情 提交于 2020-01-23 05:30:07
问题 Using a SQL 2008 R2 November release database and a .net 4.0 Beta 2 Azure worker role application. The worker role collects data and inserts it into a single SQL table with one identity column. Because there will likely be multiple instances of this worker role running, I created an Insert Instead Of trigger on the SQL table. The trigger performs Upsert functionality using the SQL Merge function. Using T-SQL I was able to verify the insert instead of trigger functions correctly, new rows were

Stored Procedure not showing fields in Crystal reports

霸气de小男生 提交于 2020-01-21 17:25:07
问题 Using: CR in VS2010, SQL Server 2008R2 I have a SP that returns results when run in SQL Server, but when I add this to a CR it doesnt show any fields in the Field Explorer. But if I try any other SP, it works fine I can see its fields. Steps I am performing: Open existing CR Right CLick Database Fields --> Database Expert --> remove the old SP --> Add the new SP A window with Enter Values pops-up, I set all to NULL and click OOK The New SP shows up in the Database expert right side I can see

How to display the date as mm/dd/yyyy hh:mm Am/PM using sql server 2008 r2?

别来无恙 提交于 2020-01-20 18:44:13
问题 My sample query is SELECT D30.SPGD30_LAST_TOUCH_Y from CSPGD30_TRACKING D30 My given date format is like "2013-01-01 00:00:00.000" . I need to convert this date format to "mm/dd/yyyy hh:mm AM/PM" . Do you have any idea about that? 回答1: I think there is no single format to give them both. Try this using Convert ; Sql-Demo declare @mydate datetime = getdate() select convert(varchar(10),@mydate, 101) + right(convert(varchar(32),@mydate,100),8) | COLUMN_0 | ---------------------- | 02/22/2013 9

How to display the date as mm/dd/yyyy hh:mm Am/PM using sql server 2008 r2?

若如初见. 提交于 2020-01-20 18:42:49
问题 My sample query is SELECT D30.SPGD30_LAST_TOUCH_Y from CSPGD30_TRACKING D30 My given date format is like "2013-01-01 00:00:00.000" . I need to convert this date format to "mm/dd/yyyy hh:mm AM/PM" . Do you have any idea about that? 回答1: I think there is no single format to give them both. Try this using Convert ; Sql-Demo declare @mydate datetime = getdate() select convert(varchar(10),@mydate, 101) + right(convert(varchar(32),@mydate,100),8) | COLUMN_0 | ---------------------- | 02/22/2013 9

How to display the date as mm/dd/yyyy hh:mm Am/PM using sql server 2008 r2?

狂风中的少年 提交于 2020-01-20 18:42:24
问题 My sample query is SELECT D30.SPGD30_LAST_TOUCH_Y from CSPGD30_TRACKING D30 My given date format is like "2013-01-01 00:00:00.000" . I need to convert this date format to "mm/dd/yyyy hh:mm AM/PM" . Do you have any idea about that? 回答1: I think there is no single format to give them both. Try this using Convert ; Sql-Demo declare @mydate datetime = getdate() select convert(varchar(10),@mydate, 101) + right(convert(varchar(32),@mydate,100),8) | COLUMN_0 | ---------------------- | 02/22/2013 9

Stored procedure with variable number of parameters

試著忘記壹切 提交于 2020-01-20 18:32:31
问题 I have stored procedure where I have to pass parameters, But the problem is I am not sure how many parameters is going to come it can be 1, in next run it can be 5. cmd.Parameters.Add(new SqlParameter("@id", id) Can anyone help how can I pass these variable number of parameters in stored procedure? Thanks 回答1: You could pass it in as a comma-separated list, then use a split function, and join against the results. CREATE FUNCTION dbo.SplitInts ( @List VARCHAR(MAX), @Delimiter CHAR(1) ) RETURNS

Sum of child levels total in a Hierarchy

若如初见. 提交于 2020-01-20 02:48:05
问题 I need to have each level be the sum of all children (in the hierarchy) in addition to any values set against that value itself for the Budget and Revised Budget columns. I've included a simplified version of my table structure and some sample data to illustrate what is currently being produced and what I'd like to produce. Sample table: CREATE TABLE Item (ID INT, ParentItemID INT NULL, ItemNo nvarchar(10), ItemName nvarchar(max), Budget decimal(18, 4), RevisedBudget decimal(18, 4)); Sample

SQL - run query against different table (dynamic table) based on parameter

女生的网名这么多〃 提交于 2020-01-17 13:47:19
问题 I basically have a query with a @date parameter, and depending on that @date I want to run a query against the proper table. If it matters, the tables are the same structure but on 2 different servers however they are linked so running from either is not an issue. So I'm looking to: IF @date = getdate() THEN SELECT * FROM server1.db1..MyTable ELSE SELECT * FROM server2.db2..MyTable_history WHERE date = @date END I'm pretty sure this can be done with dynamic sql, but the query is a little