sql-server-2005

SQL Query Add an Alternate Blank Records

安稳与你 提交于 2019-12-25 06:45:57
问题 I am using following Query to Display Records from SQL SERVER select * from orders Currently this query shows all 100 records in database, what i need is instead of 100 it should show 200 records ( so alternate blank records is fine ) It is possible can it be done ? 回答1: -- sample table declare @Order table ( orderid int, qty int ) -- add some data insert into @Order select 1, 10 union all select 2, 20 union all select 3, 30 -- cross join the query against two rows select case D.N when 1 then

TSQL select rows by one from 2 conditions

元气小坏坏 提交于 2019-12-25 06:44:35
问题 I have a table like so: ORDER_ID CODE1 CODE2 CODE3 STATUS 1 '001' 'BIGP' NULL 4 2 '002' 'BIGP' NULL 1 3 '001' NULL NULL 6 4 '002' NULL 'L' 1 and second table like so: ADDRESS_ID ORDER_ID TYPE ADD_DATE CATEGORY 1 1 'K1' '2010-01-01' 'CLIENT' 2 1 'D1' '2010-01-02' 'SYSTEM' 3 2 'D2' '2010-01-02' 'SYSTEM' 4 2 'D2' '2010-02-01' 'CLIENT' What I must do if to for every order that has: status not in (4,6) code1='002' (code2=null and code3=null) or (code2 in ('BIGA', 'BIGP') and code3=null) or (code2

TSQL select rows by one from 2 conditions

若如初见. 提交于 2019-12-25 06:43:47
问题 I have a table like so: ORDER_ID CODE1 CODE2 CODE3 STATUS 1 '001' 'BIGP' NULL 4 2 '002' 'BIGP' NULL 1 3 '001' NULL NULL 6 4 '002' NULL 'L' 1 and second table like so: ADDRESS_ID ORDER_ID TYPE ADD_DATE CATEGORY 1 1 'K1' '2010-01-01' 'CLIENT' 2 1 'D1' '2010-01-02' 'SYSTEM' 3 2 'D2' '2010-01-02' 'SYSTEM' 4 2 'D2' '2010-02-01' 'CLIENT' What I must do if to for every order that has: status not in (4,6) code1='002' (code2=null and code3=null) or (code2 in ('BIGA', 'BIGP') and code3=null) or (code2

How to get NextDayofWeek if you pass the date?

淺唱寂寞╮ 提交于 2019-12-25 06:35:14
问题 Here's what I am trying to do. I will change the following code into SP which takes two parameter @startdate, and @transactionDate, and it will return the NextTransactiondate. The logic is @startdate determine which day of the week it is. The @NexttransactionDate should be equal to the day following the transactiondate. so in this example, the startday is Wednesday so the next transaction date should be - 2011-05-04'. In the code below, it is always computing to friday, but it should be

How to show multiple row values in comma separated in a single row in SQL Server 2005?

杀马特。学长 韩版系。学妹 提交于 2019-12-25 05:09:54
问题 Below I have shown two tables and also the result table. How can I get the result table in this manner as I shown on above? 回答1: select min(ID) as ID, Val, stuff((select ','+Cat from Table2 as T2 where T1.Val = T2.Val for xml path(''), type).value('.', 'nvarchar(max)'), 1, 1, '') as Cat from Table2 as T1 group by Val order by ID SQL Fiddle 回答2: DECLARE @Table1 TABLE ( id INT ,Val VARCHAR(100) ) DECLARE @Table2 TABLE ( id INT ,Val VARCHAR(100) ,Cat VARCHAR(100) ) INSERT INTO @Table1 VALUES(1,

Sort records by time inserted

独自空忆成欢 提交于 2019-12-25 05:07:51
问题 How can I query data in the order it was created? I don't have a date-created field in this table. 回答1: If you don't have a field storing the time of insertion, or any other meta-data regarding the order of insertion, there is no reliable way to get this information. You could maybe depend on a clustered index key, but these are not guaranteed. Neither are IDENTITY fields or other auto-generated fields. To clarify, an IDENTITY field does auto-increment, but... You can insert explicit values

Local variable not available after the declaration batch

此生再无相见时 提交于 2019-12-25 04:56:52
问题 This is my very first T-SQL question, so I hope I'm not using the wrong terminology. I just learned some basic stored procedures and variable declaration, however there is something I haven't understood. DECLARE @CurrentDate DATETIME SET @CurrentDate = GETDATE() select @CurrentDate ----------------------- 2013-09-25 18:47:07.547 (1 row(s) affected) If I running the above in two batches ( declare and set together, select after) I get: DECLARE @CurrentDate DATETIME SET @CurrentDate = GETDATE()

insert data into several tables - possibly using OUTPUT - sql server 2005

被刻印的时光 ゝ 提交于 2019-12-25 04:55:34
问题 I would like to insert data from a file into the table Division of this model which is very much simplified: create table Statements ( Id INT IDENTITY NOT NULL primary key (Id) ) create table Item ( StatementFk INT not null, ItemNameFk INT not null, primary key (StatementFk) ) create table ItemNames ( Id INT not null, Name NVARCHAR(255) null unique, primary key (Id) ) create table Division ( ItemFk INT not null, primary key (ItemFk) ) alter table Item add constraint FKBCDC2F95A77158D3 foreign

Writing a dreaded SQL search query

杀马特。学长 韩版系。学妹 提交于 2019-12-25 04:53:37
问题 I am working on a search query that does not seem to work. The complete query is: set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER PROCEDURE [dbo].[usp_Item_Search] @Item_Num varchar(30) = NULL ,@Search_Type int = NULL ,@Vendor_Num varchar(10) = NULL ,@Search_User_ID int = NULL ,@StartDate smalldatetime = NULL ,@EndDate smalldatetime = NULL AS DECLARE @SQLstr as nvarchar(4000) Set @SQLstr = 'SELECT RecID, Vendor_Num, Vendor_Name, InvoiceNum, Item_Num, (SELECT CONVERT(VARCHAR(11), RecDate,

Top 5 with most friends

ⅰ亾dé卋堺 提交于 2019-12-25 04:35:14
问题 Hi I'm new to SQL and I'm trying to figure out how I'm going to get the top 5 "bands" with most friends (userId) and this is what i have; a usertbl with userId as PK then a bandsTbl with bandId as PK then I have a table bandfriends with FK userId and bandId. bandfriends userid | bandId --------------- 1 | 1 1 | 2 1 | 3 Thanks! 回答1: Read up on COUNT and GROUP BY at mysql.org You'll want something like this (I haven't tested it): SELECT bandId, COUNT(*) as fans FROM bandfriends ORDER BY fans