tsql

SQL Server 2005 function to extract a substring match following a set string

安稳与你 提交于 2020-02-05 08:13:09
问题 Anybody have a nice tight and efficient SQL Server function that will return the fist string (terminated by a whitespace) following the first match of a given string. I've got some code, but it's real ugly and probably slow. For example, in In test 12545 file:x12545.jpg appears to be good given file: would return x12345.jpg Thanks. 回答1: create function dbo.extractAfter(@full nvarchar(max), @part nvarchar(max)) returns nvarchar(max) with returns null on null input as begin return ltrim(stuff

SQL Query Help

≯℡__Kan透↙ 提交于 2020-02-05 02:32:08
问题 Duplicate: How to do a Select in a Select I have 2 tables: TABLE1 Table1Id TABLE2 Table2Id Table1Id UserId TABLE2 has thousands of entries in it. I want to return a list of TABLE1 entries where there isn't an entry in TABLE2 for it for a particular user. So, where there isn't a foreign key entry in TABLE2. A query like: select count(*) from TABLE1 where Table1Id not in ( select Table1Id from TABLE2 where id_user = 1) However, that query runs very slowly. What would be the most efficient way

TSQL query to find last execution time of a UDF

白昼怎懂夜的黑 提交于 2020-02-04 11:45:10
问题 I know of ways to determine the last execution time of a stored procedure, but are there similar methods available to determine the execution time of a user defined function? 回答1: If details are still in the cache... SELECT qs.last_execution_time FROM sys.dm_exec_query_stats qs CROSS APPLY (SELECT 1 AS X FROM sys.dm_exec_plan_attributes(qs.plan_handle) WHERE ( attribute = 'objectid' AND value = OBJECT_ID('[YourDBName].[dbo].[YourFunctionName]') ) OR ( attribute = 'dbid' AND value = DB_ID(

Creating and Selecting table variables dynamically in SQL Server stored procedure?

ぐ巨炮叔叔 提交于 2020-02-04 05:41:29
问题 Please guide me how to create table variables dynamically. I know it can be like this: DECLARE @people TABLE ( id INT, name VARCHAR(32) ); How I will create if dont know columns and data types. Scenario is I have to create table variable as per a physical tables and selct data into them, use them in SP and then return data in table variables to C# program. For example I have a table Employees. I want create a table variable with same structure as Employyes have. But mentioning columns take a

How to Obtain Domain User Name when connecting via SQL Authentication

拥有回忆 提交于 2020-02-04 05:41:09
问题 I have an application which connects to SQL Server 2000 (using a generic SQL Login and SQL Authentication). I would like to implement some logging via triggers to track data changes. I can't use USER_NAME() because that returns the generic account. I've poked through master..sysprocesses and it doesn't seem to record the username (although it does record the machine name). Any ideas how, within SQL, to gain access to the username? (Note: yes, I could pass it in as a variable via the

why is t-sql allowing me to violate a check constraint that uses a UDP?

China☆狼群 提交于 2020-02-04 04:21:05
问题 I have a check constraint on a table in my DB. My understanding of a check is it sets a logical condition that must be true for records in a table. USE [myDB] GO ALTER TABLE [dbo].[myTable] WITH CHECK ADD CONSTRAINT [oneProgramPerTest] CHECK (([dbo].[howManyProgPerTest]([TestId])<(2))) GO ALTER TABLE [dbo].[myTable] CHECK CONSTRAINT [oneProgramPerTest] GO But I am able to do an update to the table that breaks the constraint. After the update, this query returns 9 records: select COUNT (*)

Converting TSQL code to C# data type problem

…衆ロ難τιáo~ 提交于 2020-02-04 03:53:31
问题 I was trying to answer this SO question.. Given the following TSQL code DECLARE @input1 INT = 100000 DECLARE @input2 INT = 40 DECLARE @input3 INT = 106833 DECLARE @X decimal(22,6) = 0 DECLARE @Y decimal(22,6) = 0.001 DECLARE @Z decimal(22,6) DECLARE @r decimal(22,6) DECLARE @v decimal(22,6) SET @v = POWER(1/(1+ (@Y/12)), @input2) SET @r = ((@Y/@input2) * @input1) / (1-@v) IF (@r < @input3) SET @Z = @Y + ABS((@X - @Y)/2) ELSE SET @Z = @Y - ABS((@X - @Y) /2) SET @X = @Y SET @Y = @Z WHILE (ABS(

Converting TSQL code to C# data type problem

这一生的挚爱 提交于 2020-02-04 03:53:27
问题 I was trying to answer this SO question.. Given the following TSQL code DECLARE @input1 INT = 100000 DECLARE @input2 INT = 40 DECLARE @input3 INT = 106833 DECLARE @X decimal(22,6) = 0 DECLARE @Y decimal(22,6) = 0.001 DECLARE @Z decimal(22,6) DECLARE @r decimal(22,6) DECLARE @v decimal(22,6) SET @v = POWER(1/(1+ (@Y/12)), @input2) SET @r = ((@Y/@input2) * @input1) / (1-@v) IF (@r < @input3) SET @Z = @Y + ABS((@X - @Y)/2) ELSE SET @Z = @Y - ABS((@X - @Y) /2) SET @X = @Y SET @Y = @Z WHILE (ABS(

Linq .Contains with large set causes TDS error

不想你离开。 提交于 2020-02-03 18:55:28
问题 I've oversimplified this a bit, because I'm looking for a general-purpose answer. Let's say I've got a table setup like this: Parent recno int (unique, pk) date datetime stuff varchar(50) Child parentrecno (int, fk) --- PK sequence (int) --- PK data varchar(50) And in my C# program I've gone through a lot of hassle to find the Parent records that I'm interested in and stuff them into a list. Parent is a really large table, and I'd rather not query it more than necessary. So I squirrel away

Linq .Contains with large set causes TDS error

依然范特西╮ 提交于 2020-02-03 18:54:09
问题 I've oversimplified this a bit, because I'm looking for a general-purpose answer. Let's say I've got a table setup like this: Parent recno int (unique, pk) date datetime stuff varchar(50) Child parentrecno (int, fk) --- PK sequence (int) --- PK data varchar(50) And in my C# program I've gone through a lot of hassle to find the Parent records that I'm interested in and stuff them into a list. Parent is a really large table, and I'd rather not query it more than necessary. So I squirrel away