tsql

Export products and variants from SQL Server

I have a SQL Server DB that has a table of products, and another table which contains a list of the sku variants of each product if it has one. I want to export all the products and their SKU's into excel. At the moment, I have a helper SQL function which performs the subquery against a product_id and concatenates all the SKU's into a c...

T-SQL Question : Query to XML

anyone can show me how to generate from this data ------------------------DATA-------------------------- Key ParentKey 5 NULL 25 5 33 25 26 5 27 5 34 27 28 5 29 5 to this XML result? ---------------------RESULTS-------------------------- <record key="5" parentkey = ""> <record key="25" parentkey = "5"> <record key="3...

generate structured (xml) document from hierarchical table data (T-SQL)

Hello people! I have a table like this (simplified): ID | Name | Parent --------------------------------- 1 | IND | NULL 2 | INS | 5 3 | CON | NULL 4 | AUT | 1 5 | FIN | NULL 6 | PHA | 1 7 | CFIN | 5 8 | CMRKT | 7 DDL: CREATE TABLE [dbo].[tblIndustryCodes]( ...

Send bulk email from SQL Server 2008

Hi, I am relatively new to databases,so please forgive me if my query sounds trivial. I need to send bulk email to all the contacts listed in a particular table. The body of the email is stored in a .doc file which begins as Dear Mr. _. The SQL server will read this file, append the last name of the contact in the blank space and send it...

Should I use the keyword AS when aliasing columns in SQL Server?

Simple question: Office debate about whether the keyword AS is necessary in our T-SQL statements. I've always used it in cases such as SELECT mycol AS Something FROM MYTABLE; I know you don't NEED it but is there any benefit of using it (or not using it)? I think it makes statements easier to read but people seem to disagree with me. ...

SQL Server: Drop Table with FK

On table "A" depend about 30 other tables via FK to "A.Id". For integration testing I have to drop the table and recreate it to create a defined state. Because of the dependent objects their seem to be no way to delete and recreate the table. The error message is: Could not drop object 'dbo.A' because it is referenced by a FOREI...

Finding a sql query to get the latest associated date for each grouping.

I have a sql table of payroll data that has wage rates and effective dates associated with those wage rates, as well as hours worked on various dates. It looks somewhat like this: EMPID DateWorked Hours WageRate EffectiveDate 1 1/1/2010 10 7.00 6/1/2009 1 1/1/2010 10 7.25 6/10/2009 1 1/1/201...

Is it possible to write an "insert into" statement for a single column table?

Here's the caveat... If you have a table that has a single column and that column happens to be an identity column with identity_insert turned OFF, is it still possible to write a T-SQL insert statement for this table? ...

Table-level diff and sync procedure for T-SQL

I'm interested in T-SQL source code for synchronizing a table (or perhaps a subset of it) with data from another similar table. The two tables could contain any variables, for example I could have base table source table ========== ============ id val id val ---------- ------------ 0 1 0 3 ...

SQL Server binary(128) convert from little endian to big endian

how to convert a binary(128) from little endian to big endian in SQL Server? ...

SQL Server search filter and order by performance issues

We have a table value function that returns a list of people you may access, and we have a relation between a search and a person called search result. What we want to do is that wan't to select all people from the search and present them. The query looks like this SELECT qm.PersonID, p.FullName FROM QueryMembership qm INNER JOIN dbo...

How to call an extended procedure from a function

hi im having trouble trying to get the following function to work. CREATE FUNCTION test ( @nt_group VARCHAR(128) ) RETURNS @nt_usr TABLE ( [name] [nchar](128) NULL , [type] [char](8) NULL , [privilege] [char](9) NULL , [mapped login name] [nchar](128) NULL , [permission path] [nchar](128) NULL ) AS BEGIN ...

How to populate a column with a count of numbers?

I am converting to an integer primary key and am having trouble seeding the new column data with a count of integer numbers. Given an existing table: create table t1 ( Id uniqueidentifier, NewId int, Data nvarchar(100) ) How would I update existing rows with a count of numbers from 1 to the # of rows in the result set? So: |...

1 and 0 not recognized as boolean constants in T-SQL

If I try to run this query in SQL Server 2005: SELECT 1 WHERE NOT ( 0 ) I get this error: Msg 4145, Level 15, State 1, Line 1 An expression of non-boolean type specified in a context where a condition is expected, near ')'. Sometimes when debugging complex WHERE statements I will cut out pieces and test a particular scenar...

Why does SQL Server stall after some of the rows have been returned during a select statement?

I have a simple procedure that selects 1000 rows from a table. For the sake of clarity, here's what the stored procedure looks like: alter procedure dbo.GetDomainForIndexing @Amount int=1, @LastID bigint, @LastFetchDate datetime as begin select top (@Amount) * from DomainName with(readuncommitted) where LastUpdated > ...

T-SQL Table Variable Creating PHYSICAL Table!

OMG! What am I doing wrong? declare @WTF TABLE ( OrderItemId int ) SELECT TOP 20 OrderItemId as OrderItemId INTO [@WTF] FROM ac_OrderItems SELECT * FROM [@WTF] Problem A: This creates a PHYSICAL table called @WTF. WHY?? I thought this was in memory only?! Problem B: The last line of code, if I do select * from @WTF... WITHOUT the ...

How to prevent out-of-range datetime value error?

I am passing an ad-hoc Insert statement from c# application to the sql server 2000/2005. Sometimes, if machine (where sql server is installed) datetime format is different than what I am passing in, it is throwing an error. e.g. : In Insert statement I am passing '2010-03-10 00:00:00-05:00' this but machine regional date setting is dif...

SQL query to fetch products with most top 1 most recent price change.

I'm using SQL Server 2005. Let's say I have a table for products and another table for prices so that I can track price changes over time. I need a query that fetches distinct products (easy part) plus each product's most recent price and the date it changed. Products Table: CREATE TABLE [dbo].[Products]( [ID] [int] IDENTITY(1,1)...

How to determine the maximum id of a set of tables in my database

I have a requirement to determine the maximum Id int value for a set of tables in my database. The column is always 'Id' and is the primary key. Is there a simple way I can make this determination without resorting to a cursor or looping? ...

pivoting on a column without knowing the entire list of values

I have a table Title Name Type ------------------------------------------------ T1 A Primary T1 B Primary T2 B Primary T2 C Secondary T2 D Secondary I need the output t...