Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NEED SQL HElP?

Status
Not open for further replies.

Parula

Programmer
May 10, 2001
18
US
Hi everyone

This is my SQL statement for a append query. i would like to put in a funtion that asks the user how many number shold be appended before it run.Is there a way to it?

INSERT INTO an_sent ( cust_salutation, cust_fname, cust_lname, cust_addr, cust_city, cust_state, cust_zip, c_type, c_cnum, date_sent, cust_in_sent )
SELECT customers.cust_salutation, customers.cust_fname, customers.cust_lname, customers.cust_addr, customers.cust_city, customers.cust_state, customers.cust_zip, transaction.c_type, transaction.c_cnum, transaction.date_sent, customers.cust_in_sent
FROM customers INNER JOIN transaction ON customers.cust_phone = transaction.cust_phone
GROUP BY customers.cust_salutation, customers.cust_fname, customers.cust_lname, customers.cust_addr, customers.cust_city, customers.cust_state, customers.cust_zip, transaction.c_type, transaction.c_cnum, transaction.date_sent, customers.cust_in_sent
HAVING (((customers.cust_zip)=[Enter in a zip code]) AND ((transaction.c_type)="AN"));
 

If you dynamically build the query you can use the Top N clause to select the number of inserts. Unfortunately, Top N doesn't accept parameter input.

INSERT INTO an_sent ( cust_salutation, cust_fname, ... )
SELECT TOP 25 customers.cust_salutation, customers.cust_fname, ...
FROM ...
WHERE customers.cust_zip=[Enter in a zip code] AND transaction.c_type="AN"
GROUP BY ...;


NOTE: I recommend using a WHERE clause rather than HAVING to select records. The WHERE selection occurs before aggregation whereas the HAVING selection occurs after. Selection criteria should occur in the WHERE clause unless the selection is based on an aggregate value. In addition, your query will run faster. Terry

X-) "Life would be easier if I had the source code." -Anonymous
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top