Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-02 : 06:23:13
|
| Hi!!How can i make a stored proc that Delete then insert records that has loop that inserts a LIMIT of 10 records only?even the records that i retrieved is only 5 rows then the remaining 5 rows is null values.. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-02 : 06:30:51
|
Question :1. Why delete than insert ? Can you just use UPDATE instead ?2. Why loop ?quote: the records that i retrieved is only 5 rows then the remaining 5 rows is null values.
just check for NULL value and don't insert itinsert into table ( . . .)select . . .from sometablewhere somecol is not null KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-02 : 06:48:10
|
if i select * from table if the results is 5 records only then make it 10 to be fixed but the remaining last five is to be insert is null..so it makes ten records.. |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-02 : 07:43:31
|
| please help here!anyone can help me? |
 |
|
|
rohitkumar
Constraint Violating Yak Guru
472 Posts |
Posted - 2009-07-02 : 07:46:24
|
| you dont actually have to insert it in the table, if you want the resultset to be always 10 records then join it with a temp table/subquery with 10 records. show us what you have done so far and we can help you further |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-02 : 08:32:10
|
[code]insert into yourtable( . . .)select < . . . >from ( select row = 1 union all select row = 2 union all . . . . select row = 10 ) n left join ( select row_no = row_number() over (order by somecol), < other col of you result> from sometable ) r on n.row = r.row[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-02 : 09:04:25
|
| for example i have this table name tblinfoCustomer_First_NameCustomer_Last_NameAddressTelephone_Number------------actually rohitkumar that's the way i thought to create a temporary table..my problem is how to make that loop to maintain ten records even there is only five records to insert..@khtan please explain what you had post.. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-02 : 09:09:32
|
[code]insert into tblinfo (Customer_First_Name, Customer_Last_Name, Address, Telephone_Number)select r.Customer_First_Name, r.Customer_Last_Name, r.Address, r.Telephone_Numberfrom ( select row = 1 union all select row = 2 union all . . . . select row = 10 ) n left join ( select row_no = row_number() over (order by somecol), Customer_First_Name, Customer_Last_Name, Address, Telephone_Number from sometable ) r on n.row = r.row[/code]the first derived table n creates 10 records. In 2nd derived table r, there is a row_number() to create the record row no. When it left join to the 2nd table, it will always insert 10 records into tblinfo. And for those rows not in your result, the value will be NULL as what you wanted. KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-07-02 : 12:28:46
|
you can use internal count table if you dont want to give values explicitlyinsert into tblinfo (Customer_First_Name, Customer_Last_Name, Address, Telephone_Number)select r.Customer_First_Name, r.Customer_Last_Name, r.Address, r.Telephone_Numberfrom master..spt_values n left join ( select row_no = row_number() over (order by somecol), Customer_First_Name, Customer_Last_Name, Address, Telephone_Number from sometable ) r on n.number = r.rowwhere n.type='p'and n.number between 1 and 10 |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-02 : 20:58:51
|
@Visakh and @khtaninsert into TEMP_TABLE (CustomerFN, CustomerLN, Address, TelNo)select r.Customer_First_Name, r.Customer_Last_Name, r.Address, r.Telephone_Numberfrom master..spt_values n left join ( select row_no = row_number() over (order by Customer_First_Name, Customer_Last_Name, Address, Telephone_Number), Customer_First_Name, Customer_Last_Name, Address, Telephone_Number from CustomerDetails ) r on n.number = r.row --Error Partwhere n.type='p'and n.number between 1 and 10 i tried the code you post but ihave an error:Msg 207, Level 16, State 1, Line 9Invalid column name 'row'. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-02 : 21:06:52
|
fixed typoquote: Originally posted by visakh16 you can use internal count table if you dont want to give values explicitlyinsert into tblinfo (Customer_First_Name, Customer_Last_Name, Address, Telephone_Number)select r.Customer_First_Name, r.Customer_Last_Name, r.Address, r.Telephone_Numberfrom master..spt_values n left join ( select row_no = row_number() over (order by somecol), Customer_First_Name, Customer_Last_Name, Address, Telephone_Number from sometable ) r on n.number = r.row_nowhere n.type='p'and n.number between 1 and 10
KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-02 : 23:20:17
|
| Thanks Guys for helping..You are the man..you help a lot of DBA and even programmers here..it works.. |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-09 : 01:36:21
|
| Hi!just want to open this topic again!i just want to fill some fields just like thisCustomer_Last_Name, i want this Field all same value? even some of the some fields are NULL.. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-07-09 : 01:39:46
|
Please explain more in details . . KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-09 : 01:46:17
|
| example i got 1-5 records then the 5 remaining are NULL(6-10 rows) but i want the all Customer_Last_Name field same with the first record.. |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-09 : 02:04:10
|
i have this criteria: insert into tblinfo (Customer_First_Name, Customer_Last_Name, Address, Telephone_Number)select r.Customer_First_Name, r.Customer_Last_Name, r.Address, r.Telephone_Numberfrom master..spt_values n left join ( select row_no = row_number() over (order by somecol), Customer_First_Name, Customer_Last_Name, Address, Telephone_Number from sometable CustomerID = @CustomerID ) r on n.number = r.row_nowhere n.type='p'and n.number between 1 and 10 |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-09 : 02:25:16
|
| or just this[code]SELECT * FROM TEMP_BIR2307_MOP0UNION ALLSELECT * FROM TEMP_BIR2307_MOP1[code]then select TOP 1 record..How to select the TOP 1 record using with that UNION ALL? |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-09 : 02:27:33
|
| Solved it already..thanks |
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-13 : 21:59:59
|
hi everyone!i have this code but got me error in GROUP BYinsert into TEMP_BIR2307_MOP0 ( ATCCode,SupplierID, CVAmount,CreditAmount, Description,SupplierName, Address1,Address2, TIN,ModeOfPayment )SELECT r.ATCCode,r.SupplierID, SUM(r.CVAmount) AS CVAmount, SUM(r.CreditAmount) AS CreditAmount, r.Description,r.SupplierName, r.Address1,r.Address2, r.TIN,r.ModeOfPaymentfrom master..spt_values n left join ( select row_no = row_number() OVER(ORDER BY ATCCode), ATCCode,SupplierID, CVAmount,CreditAmount, Description,SupplierName, Address1,Address2, TIN,ModeOfPayment FROM dbo.vwGetATCSummary WHERE CVDate >= '07/01/2009' AND SupplierID = 22 GROUP BY ATCCode,SupplierID, CVAmount,CreditAmount, Description,SupplierName, Address1,Address2, TIN,ModeOfPayment ) r on n.number = r.row_nowhere n.type='p'and n.number between 1 and 13 But got me an errorMsg 8120, Level 16, State 1, Line 4Column 'r.ATCCode' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Please help.. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-07-13 : 22:12:03
|
quote: Originally posted by chriztoph hi everyone!i have this code but got me error in GROUP BYBEGINinsert into TEMP_BIR2307_MOP0 ( ATCCode, SupplierID, CVAmount, CreditAmount, Description, SupplierName, Address1, Address2, TIN, ModeOfPayment )SELECT r.ATCCode, r.SupplierID, SUM(r.CVAmount) AS CVAmount, SUM(r.CreditAmount) AS CreditAmount, r.Description, r.SupplierName, r.Address1, r.Address2, r.TIN, r.ModeOfPaymentfrom master..spt_values n left join ( select row_no = row_number() OVER(ORDER BY ATCCode), ATCCode, SupplierID, CVAmount, CreditAmount, Description, SupplierName, Address1, Address2, TIN, ModeOfPayment FROM dbo.vwGetATCSummary WHERE CVDate >= '07/01/2009' AND SupplierID = 22 GROUP BY ATCCode, SupplierID, CVAmount Description, SupplierName, Address1, Address2, TIN, ModeOfPayment ) r on n.number = r.row_nowhere n.type='p'and n.number between 1 and 13END But got me an errorMsg 8120, Level 16, State 1, Line 4Column 'dbo.vwGetATCSummary.CVAmount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Please help..
|
 |
|
|
chriztoph
Posting Yak Master
184 Posts |
Posted - 2009-07-13 : 22:16:05
|
| please again the last that i posted becasue i edited it... |
 |
|
|
Next Page
|
|
|
|
|