sql-server-2005

Nested if statements in SQL Server stored procedure SELECT statement

我只是一个虾纸丫 提交于 2019-12-24 18:35:48
问题 I'm new here, and relatively new to stored procedures, so please bear with me! I've checked related questions on here and can't find anything that works in this instance. I am trying to build a stored procedure (MS SQL Server 2005) that takes a number of passed in values and in effect dynamically builds up the SQL as you would with inline SQL. This is where I've come unstuck. We have (somewhat simplified for clarity): @searchf1 varchar(100), -- search filter 1 @searchr1 varchar(100), --

how to handle optional parameters in a update stored procedure

谁都会走 提交于 2019-12-24 18:30:26
问题 I need to write a generic procedure that set value for one column or no of a columns in a table depending on parameters. Any idea how to do it. 回答1: I would guess you want something like: CREATE PROC UpdateProc @RowID UNIQUEIDENTIFIER, @Parameter1 NVARCHAR(50) NULL, @Parameter2 INT NULL AS SET NOCOUNT ON GO IF @Parameter1 IS NOT NULL BEGIN UPDATE MyTable SET Column1 = @Parameter1 WHERE ID = @RowID END IF @Parameter2 IS NOT NULL BEGIN UPDATE MyTable SET Column2 = @Parameter2 WHERE ID = @RowID

Why is this SQL Server query deadlocking?

隐身守侯 提交于 2019-12-24 17:43:33
问题 Can someone tell me why the following SQL Server queries is deadlocking and what is the solution to fixing it? <deadlock-list> <deadlock victim="process88b5b8"> <process-list> <process id="process88b5b8" taskpriority="0" logused="76132" waitresource="RID: 32:1:151867:174" waittime="5093" ownerId="65554098" transactionguid="0xedf3314c05f1124cbe8d480cd092e03e" transactionname="DTCXact" lasttranstarted="2011-09-02T19:00:29.690" XDES="0x1029e040" lockMode="S" schedulerid="1" kpid="5108" status=

SQL Server Select COUNT without using aggregate function or group by

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 16:42:47
问题 I'm in a very, very tight situation here. I have an SQL query running on SQL Server 2005: SELECT col1,col2,col3 FROM myTable Which of course gives: col1 | col2 | col3 ------------------ 1 | a | i 2 | b | ii etc I need to, if possible, add a COUNT query so that it will return the number of records returned. I cannot use GROUP BY or an aggregate function (It's a very edge case on some very inflexible software). Ideally, something like this: SELECT col1,col2,col3,COUNT(NumberOfRows) as NumRows

How to store multiple message threads in CRM database

旧城冷巷雨未停 提交于 2019-12-24 16:39:56
问题 I'm working with a Customer Relationship Management application. I have two tables: tbl_Inquiry_master for storing inquiry question details raised by end users, and tbl_Inquiry_History for storing inquiry answer details. However this shows knowledge to store one question store to table tbl_Inquiry_master and answers that are given by staff to that inquiry stored in table tbl_Inquiry_History . For more information I represent table tbl_Inquiry_master schema: Column Name Data Type _____________

How to pass sql function parameter by value

我与影子孤独终老i 提交于 2019-12-24 16:34:08
问题 This is part of a bigger selection, but I have stripped it down to the essential question : Compare this two SQL queries - the first works with a constant, the second with a variable, both have the same value (lets say 180). The one with the constant displays result immediately (e.g. within milliseconds), the one with the variable takes a few seconds to yield the same result. Where is the catch ? Query 1: SELECT * FROM table WHERE field > 180 Query 2: DECLARE @V INT SET @v = 180 SELECT * FROM

Bypass last INNER JOIN in query

对着背影说爱祢 提交于 2019-12-24 16:11:00
问题 I have the following stored procedure which takes a user ID, a starting date, an end date, and a list of codes in a comma-delimited list, and it returns all activity records between those two dates which match one of the codes in the list. ALTER PROCEDURE [dbo].[ActivitiesSummary] @UserID varchar(30), @StartDate datetime, @EndDate datetime, @Codes varchar(100) AS BEGIN SET NOCOUNT ON; SELECT act.SectionID, act.UnitID, act.ActivityCode FROM dbo.Activities act INNER JOIN ConvertCodeListToTbl(

How to determine if database exists on linked server?

浪尽此生 提交于 2019-12-24 15:35:45
问题 I know you can do something like: select count(*) as Qty from sys.databases where name like '%mydatabase%' but how could you do something like: select count(*) as Qty from linkedServer.sys.databases where name like '%mydatabases%' I guess I could put a stored procedure on the linked server and execute the first select, but is there a way to query a linked server for what databases it holds? 回答1: Assuming your linked server login has read permissions on the master.sys.databases table, you can

Need to capture database name when error occurs with sp_msforeachdb

微笑、不失礼 提交于 2019-12-24 15:35:15
问题 I am running a dynamic sql command with sp_msforeachdb for each database. However the command bombs for a certain database. How is '?' used to display the database name when the error happens? I tried using it in a Catch statement but my syntax is wrong. 回答1: Just use DB_NAME() EXEC sp_msforeachdb 'USE ? SELECT DB_NAME() ...do stuff' 回答2: It worked to me: exec sp_MSforeachdb 'select *, print ''?'' from TABLE' 回答3: Depending on the script, you get for all db's the ouput "master" with DB_NAME()

Create T-SQL Function with table parameter

∥☆過路亽.° 提交于 2019-12-24 14:21:32
问题 I needed to write function with table parameter. an example: CREATE FUNCTION getParentByBrandList ( @_BrandList TABLE( BR_ID INT, BR_Name NVARCHAR(150), BR_ParentBrandID INT, BR_MasterBrandID INT, BR_Role INT, BR_State INT, BR_OwnerID INT, BR_OwnerIP NVARCHAR(50), BR_CreateDate DATETIME, BR_UpdaterID INT, BR_UpdaterIP NVARCHAR(50), BR_UpdateDate DATETIME ) ) How can I do? Thanx 回答1: Beginning from SQL Server 2008 you can use table valued parameters: CREATE TYPE [dbo].[TableType] AS TABLE( [ID