sql-server-2008-r2

Using NEWID() with CTE to produce random subset of rows produces odd results

对着背影说爱祢 提交于 2019-12-01 11:12:46
I'm writing some SQL in a stored procedure to reduce a dataset to a limited random number of rows that I want to report on. The report starts with a Group of Users and a filter is applied to specify the total number of random rows required ( @SampleLimit ). To achieve the desired result, I start by creating a CTE (temp table) with: The top(@SampleLimit) applied group by UserId (as the UserID appears multiple times) order by NEWID() to put the results in a random order SQL: ; with cte_temp as (select top(@SampleLimit) UserId from QueryResults where (GroupId = @GroupId) group by UserId order by

Unpivot ALL Columns in a SQL Table

瘦欲@ 提交于 2019-12-01 10:54:55
I have a table with 30 columns and I want to easily unpivot ALL columns. I understand I can use this strategy: SELECT col, value INTO New_Table FROM (SELECT * FROM Test_Data) p UNPIVOT (value FOR col IN (Column_Name1, Column_Name2... Column_Name30)) as unpvt This is how my data comes in: Column_Name1 Column_Name2 Column_Name3 Value11 Value21 Value31 Value12 Value22 Value32 Value13 Value23 Value33 This is how I want to store it in the new table: New_Column1 New_cloumn2 Column_Name1 Value11 Column_Name1 Value12 Column_Name1 Value13 Column_Name2 Value21 ... But there must be an easier way than

Refresh data from stored procedure

纵饮孤独 提交于 2019-12-01 10:48:41
I have a c# entity framework application. I am trying to run a stored procedure from code (no problem with that). its long running, around 30 mins. I write a log of each transaction to a SQL table as the process goes through. I am looking to initiate the procedure from the app but then show the last 10 records of the log on screen maybe re querying every 10 seconds. this will show the progress. private void Window_Loaded_1(object sender, RoutedEventArgs e) { Task.Run(() => _serviceProduct.RefreshAllAsync()); _cvsLog = (CollectionViewSource)(FindResource("cvsLog")); var dispatcherTimer = new

Calling Managed Code From Unmanaged C

拜拜、爱过 提交于 2019-12-01 10:40:15
So after about a day and a half with this I've made zero progress. I need to write a DLL in C that is used a plugin for an existing application. The DLL has to be compiled by the Visual Studio 2008 compiler with the following options cl -DNT40 -DPOMDLL -DCRTAPI1=_cdecl -DCRTAPI2=cdecl -D_WIN32 -DWIN32 -DWIN32_LEA N_AND_MEAN -DWNT -DBYPASS_FLEX -D_INTEL=1 -DIPLIB=none -I. -I"C:\plm\2T-RAC\TcEx press53\include" -I"C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include" -c -nologo -EHsc -W1 -Ox -Oy- -MD C:\mydir\myDll.c It's then linked to applications library's. What it actually needs to do

Calculate Actual Downtime ignoring overlap in dates/times

流过昼夜 提交于 2019-12-01 10:12:13
I'm trying to work out how to calculate actual downtime for various applications from data I'm storing within a table. At the moment I'm just calculating the difference between DowntimeStart and DowntimeEnd which is shown in the DowntimeMinutes. The problem is that if there is a cross-over in times as separate components are down, it should count the total ignoring the over-lap. What I expect is shown in the Expected column. Any ideas on how a query could be put together to achieve this? Application DowntimeStart DowntimeEnd DowntimeMinutes Expected Application Demo 2014-11-20 17:31:01.467

Unpivot ALL Columns in a SQL Table

纵然是瞬间 提交于 2019-12-01 09:55:54
问题 I have a table with 30 columns and I want to easily unpivot ALL columns. I understand I can use this strategy: SELECT col, value INTO New_Table FROM (SELECT * FROM Test_Data) p UNPIVOT (value FOR col IN (Column_Name1, Column_Name2... Column_Name30)) as unpvt This is how my data comes in: Column_Name1 Column_Name2 Column_Name3 Value11 Value21 Value31 Value12 Value22 Value32 Value13 Value23 Value33 This is how I want to store it in the new table: New_Column1 New_cloumn2 Column_Name1 Value11

Can we install Express edition and Standard edition of SQL Server on same PC User?

↘锁芯ラ 提交于 2019-12-01 09:39:57
I am using both SQL Server 2008 and 2008 R2, I have a situation where I need both on the same computer and in same user. But with the different versions. I wanted to installed SQL Server 2008 Express and SQL Servre 2008 R2 Standard Edition. My question is can we do this? if yes then can you please guide me how to install it. Thanks in advanced. Yes of course you can do this. Also, there's nothing special to "guide you" for installation - just install them. The only point you need to be aware of: if you have multiple versions of SQL Server installed, they have to have a separate instance name

How often is Stored Procedure xyz, or Table Valued Function Used – SQL Server 2008 R2

一笑奈何 提交于 2019-12-01 09:18:22
We have a number of Views, Stored Procs, Table Values and Scalar functions. How can we see how often or even how many times these have been called from within SQL Server? Do we need to edit each one to update a table on each call to get this, or does SQL server keep this information somewhere? This is one of Glenn Berry's DMV queries. It counts how many times a cached stored procedure has been executed (filtered by the current database): SELECT TOP(25) p.name AS [SP Name], qs.execution_count, ISNULL(qs.execution_count/DATEDIFF(Second, qs.cached_time, GETDATE()), 0) AS [Calls/Second], qs.total

Getting error in SQL query with function use

旧巷老猫 提交于 2019-12-01 08:10:29
When I run this query through C# adapter it is causing an error: Incorrect syntax near Use Any ideas? When I run this in SQL Server 2008 R2 it's working fine. create FUNCTION [dbo].[fn_Split] (@sep nvarchar(10), @s nvarchar(4000)) RETURNS table AS RETURN ( WITH Pieces(pn, start, stop) AS ( SELECT 1, 1, CHARINDEX(@sep, @s) UNION ALL SELECT pn + 1, stop + (datalength(@sep)/2), CHARINDEX(@sep, @s, stop + (datalength(@sep)/2)) FROM Pieces WHERE stop > 0 ) SELECT pn, SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END) AS value FROM Pieces ) ; /****** Object: Table [dbo].

Calling Managed Code From Unmanaged C

╄→尐↘猪︶ㄣ 提交于 2019-12-01 07:39:19
问题 So after about a day and a half with this I've made zero progress. I need to write a DLL in C that is used a plugin for an existing application. The DLL has to be compiled by the Visual Studio 2008 compiler with the following options cl -DNT40 -DPOMDLL -DCRTAPI1=_cdecl -DCRTAPI2=cdecl -D_WIN32 -DWIN32 -DWIN32_LEA N_AND_MEAN -DWNT -DBYPASS_FLEX -D_INTEL=1 -DIPLIB=none -I. -I"C:\plm\2T-RAC\TcEx press53\include" -I"C:\Program Files (x86)\Microsoft Visual Studio 8\VC\include" -c -nologo -EHsc -W1