sql-server

SQL HAVING SUM GROUP BY

Using SQL Server 2005. I am building an inventory/purchasing program and I’m at the point where I need the user to “check out” equipment. When he selects a product, I need to query which stock locations have the available Qty, and tell the user which location to walk to/ retrieve product. Here is a query for a particular [StockLocation_...

Arithmetic Overflow on numeric AVG() SQL Server

Why does select avg(cast(5 as numeric(15,2))) returns "Arithmetic overflow error converting numeric to data type numeric."? Documentation states the following about the avg function: input: decimal category (p, s) output: decimal(38, s) divided by decimal(10, 0) I don't really see how it would overflow. ...

Best practices for moving data using triggers in SQL Server 2000

Hello Friends, I have some troubles trying to move data from SQL Server 2000 (SP4) to Oracle10g, so the link is ready and working, now my issue is how to move detailed data, my case is the following: Table A is Master Table B is Detail Both relationed for work with the trigger (FOR INSERT) So My query needs to query both for create a ...

Conditional operator in Transact-sql

Is there a way to do this shorter, for instance using some sort of conditional operator in Transact-sql? IF @ParentBinaryAssetStructureId = -1 BEGIN SET @ParentBinaryAssetStructureId = NULL END UPDATE BinaryAssets.BinaryAssetStructures SET ParentBinaryAssetStructureId = @ParentBinaryAssetStructureId WHERE BinaryAssetStructu...

.Net API for SQL Service Broker

Is there a .Net API or object model for using SQL Service Broker? ...

SQL Server 2008: Count Number of Keys In Multiple Date Ranges Across Multiple Tables

Thanks to the people who answered my question this morning: http://stackoverflow.com/questions/2087664/sql-server-2008-count-number-of-keys-in-multiple-date-ranges This is an extension of it and I'm not clear how to generalize the previous solutions to this case. The database I'm working with has medications, lab values, and diagnoses...

Is it possible to auto-indent a query in Sql Server 2008 Management Studio Express?

I mean, something equivalent to Ctrl + K + D from Visual Studio that automatically indents code. Is there something in this sense? ...

SQL Server 2008 & Visual Web Developer 2008 express editions: Connection to DB Fail!

So I saw this question/comment... http://stackoverflow.com/questions/1625991/how-to-integrate-sql-server-2005-express-edition-to-visual-web-developer-2008-exp/1626114#1626114 And this is EXACTLY what I am having trouble with, only using SQL Server 2008 express edition and Visual Web Dev 2008 express. Everytime I try to click to add a ...

How to use CTE to map parent-child relationship?

Say I have a table of items representing a tree-like structured data, and I would like to continuously tracing upward until I get to the top node, marked by a parent_id of NULL. What would my MS SQL CTE (common table expression) look like? For example, if I were to get the path to get to the top from Bender, it would look like Comedy ...

Sorting with indexes

My query returns a query result which appears to be sorted, though there is no order by statement available. This could be coz of the clustered index. Is it wise to depend upon index for sorting? Is it a best practice to rely on index for sorting? Thanks ...

Remote login to SQL Server Express 2008

Hey. I am remotely accessing SQL Server Express 2008. I have turned on service "SQL Server Browser" I have turned on service "SQL Server (SQLExpress)" I have enabled TCP/IP and set dynamic ports to blank and tcp port to 1433 in properties I can connect localy(using ip) so my credentials are correct. I have a exceptions record in my wi...

SQL Server query : SELECT 1 WHERE EXISTS versus SELECT TOP 1 1

I need to present a flag - 0 if a condition is not meet, 1 if it is - and I can do it in two different ways : Get Employee ID, name, 1 if has others in suborder - 0 if not : SELECT e.ID , e.Name , ISNULL ( ( SELECT TOP 1 1 FROM Employee se WHERE se.ManagerID = e.ID ) , 0 ) AS HasSubordinates FROM Employee e or SELECT ...

How to elegantly write a SQL ORDER BY (which is invalid in inline query) but required for aggregate GROUP BY?

I have a simple query that runs in SQL 2008 and uses a custom CLR aggregate function, dbo.string_concat which aggregates a collection of strings. I require the comments ordered sequentially hence the ORDER BY requirement. The query I have has an awful TOP statement in it to allow ORDER BY to work for the aggregate function otherwise th...

Find time duration

I have two date fields with times.They are 1.2008-06-09 10:18:00.000 2.2008-06-10 11:20:00.000 I have to find the difference between the above two dates in the format - "24 hour:2 minutes:0 seconds" Is there any method to get this result in sql 2000? ...

Cannot find either column "dbo" or the user-defined function or aggregate "dbo.Splitfn", or the name is ambiguous.

Hai guys, I ve used the following split function, CREATE FUNCTION dbo.Splitfn(@String varchar(8000), @Delimiter char(1)) returns @temptable TABLE (items varchar(8000)) as begin declare @idx int declare @slice varchar(8000) select @idx = 1 if len(@String)<1 or @String is null retur...

SSIS: Cannot put a SQL Task within a Foreach Loop

I am new to SSIS and I am relying on a book, so this should be an easy question to answer. I want to import data from all of the text files within a directory. So I create a Foreach container. The first thing I want to do within the loop is execute a SQL task to drop and create the landing table. Then I want to load the data into the ...

Regarding SQL Server Locking Mechanism

Dear GURUs I would like to ask couple of questions regarding SQL Server Locking mechanism If i am not using the lock hint with SQL Statement, SQL Server uses PAGELOCK hint by default. am I right??? If yes then why? may be its due to the factor of managing too many locks this is the only thing i took as drawback but please let me know ...

Return first 100 letters from database field?

In my database MYDB i have a table called MYTABLE and inside it I have a column called NAME. I want to only return the first 100 characters of the column NAME. (NAME can be up to 2000 characters). How can this be done in SQL as I want to set the first 100 characters to a ASP.NET label. Thanks in advanced! ...

How Select top row from child table

i have two tables and i want to select top row from child table? there are relation by two tables by id. what can i do please help me???? ...

Return value from database without the HTML tags?

In my database MYDB I have a table called MYTABLE and I have a column called Description. I am saving a long description in there with multiple HTML tags. How can i return the values and not include all the HTML tags? Is this even possible? What will be the best way of doing this? In the SQL statement or in code behind? And how will I ...