Attempting to pull access form information into table with SQL - sql

Currently, I am attempting to make a query that pulls the user's entry from an MS Access form and puts that entry into a table for later usage. This is what I have come up with so far, but I can't get it to save so hopefully this is either correctable or there is a different approach to this problem.
INSERT INTO tempOrder ('ProductID')
VALUES(
SELECT ProductID
FROM Product
WHERE ProductName is FORMS!Sandwich!sandwichChoice1
);

for insert select you should use
INSERT INTO tempOrder (ProductID)
SELECT ProductID
FROM Product
WHERE ProductName = FORMS!Sandwich!sandwichChoice1
and use = for compare

Related

Temp table not running

Whenever I run this temp table in google bigquery sql i get "use of create temporary table requires a script"?
CREATE TEMP TABLE products AS
SELECT product, product_color,
FROM `radiant-oven-328313.customer_data.customer_purchase`
WHERE product = "fan";
Does this mean I need to insert data into my table or is my syntax correct??
To insert into my table, I've tried the additional line of code below:
Select product_color as White_color
FROM products
WHERE product_color = "white"
FROM `radiant-oven-328313.customer_data.customer_purchase`
Any help would be much appreciated, thanks!
You will need to specify the name and types for the columns.
For example:
CREATE TEMP TABLE product (product STRING, product_color STRING)
AS
SELECT product, product_color
FROM `radiant-oven-328313.customer_data.customer_purchase`
WHERE product="fan"
;
This will let you create a temporary table and interact with it within your session. However if you are not using this as part of a script already and just need it as part of your SQL Query logic you may want to consider using a CTE or sub-query instead.
For more information on temp tables:
https://cloud.google.com/bigquery/docs/reference/standard-sql/scripting-concepts#temporary_tables
For more information on CTEs:
https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#with_clause

Trying to append a query onto a temporary table

Using SQL Server 2008-R2
I have a csv of purchase IDs and in my database there is a table with these purchase IDs and there corresponding User IDs in our system. I need these to run a more complicated query after that using. I tried to bulk insert or run import wizard but I don't have permission. My new idea is to create a #temp using SELECT INTO and then have the query inside that like below.
SELECT *
INTO ##PurchaseIDs
FROM
(
SELECT PurchaseID, UserID, Added
FROM Users
WHERE PurchaseID IN (
/* These are the csv IDs just copied and pasted in */
'49397828',
'49397883',
etc.
What happens is that there are ~55,000 IDs so I get this error.
The query processor ran out of internal resources and could not
produce a query plan. This is a rare event and only expected for
extremely complex queries or queries that reference a very large
number of tables or partitions. Please simplify the query. If you
believe you have received this message in error, contact Customer
Support Services for more information.
It works if I upload about 30,000 so my new plan is to see if I can make a temp table, then append a new table to the end of that. I am also open to other ideas on how to accomplish what I am looking to do. I attached an idea of what I am thinking below.
INSERT *
INTO ##PurchaseIDs
FROM (
SELECT PurchaseID, UserID, Added
FROM Users
WHERE PurchaseID IN (
/* These are the OTHER csv IDS just copied and pasted in */
'57397828',
'57397883',
etc.
You need to create a temp table and insert the values in IN clause to the temp table and Join the temp table to get the result
Create table #PurchaseIDs (PurchaseID int)
insert into #PurchaseIDs (PurchaseID)
Select '57397828'
Union All
Select '57397828'
Union All
......
values from csv
Now use Exists to check the existence of PurchaseID in temp table instead of IN clause
SELECT PurchaseID,
UserID,
Added
FROM Users u
WHERE EXISTS (SELECT 1
FROM #PurchaseIDs p
WHERE u.PurchaseID = p.PurchaseID)

Selecting parameter from values of a SQL Query

I need to produce a report that selects a set of values from a database table based on a company. I would like the user to be able to select the name of the company from a list of available companies. The companies have 2 associated unique database ID code number. Based on what companies the user selects I need the sql query to pass to the parameter both unique codes to the parameter.
So, in short, how do I create a sql query that would show the company names and when selecting the company would then select both unique codes based on the company name I select from a single select drop down. Use the value selected from that drop-down list to run the SQL query in the report itself?
Thanks for any help or advice you can offer!
Pass the company name to to your stored proc instead of the two unique codes, then find the codes for the company inside your procedure.
CREATE someProc (#Company VARCHAR(100))
AS BEGIN
DECLARE #ID1 INT, #ID2 INT
SELECT #ID1 = someID1, #ID2 = someID2
FROM someDatabase
WHERE companyName=#Company

Correct this sql statement for Insert into, select, delete

INSERT INTO Checkout(ProductID, MemberID, Quantity)
SELECT ProductID, MemberID, Quantity
FROM Cart
WHERE (MemberID = MemberID)
DELETE FROM CART WHERE (MemberID = MemberID)
The sql statement is workable until the Select, WHERE part but once I add the delete statement then the error (syntax error missing operator in query expression) comes out. The second MemberID is a value I gave for first MemberID parameter. Please help me to solve this as after I transfer the data from first table to second table then I need to delete the data from first table.
[Edit] Thanks to you all suggestions and now I realized that for WHERE (MemberID = MemberID)
will import entire table to new table and this statement also will delete the entire table contents. What can I do to make sure that the sql statement only delete the particulars members item?
Main Page>Member login page>Buy something>Item stored in shopping cart database (with member id)>display only particular member's bought item in shopping cart> proceed to checkout(only display that member item, delete shopping cart item)
The problem is with the second "MemberID". This needs to be a constant or have a variable name that doesn't conflict with the field name. A number of techniques for disambiguation are shown in a related SO post: How do you avoid column name conflicts?
You didn't mention what version of SQL Server you're using - but you might be able to use the OUTPUT clause for the DELETE statement to achieve what you're looking for. The OUTPUT clause was introduced in SQL Server 2005, so if you're on 2005 or newer, this code snippet should work for you:
Try this:
DELETE FROM dbo.CART
OUTPUT deleted.ProductID, deleted.MemberID, deleted.Quantity
INTO dbo.Checkout(ProductID, MemberID, Quantity)
WHERE MemberID = #MemberID
Basically this runs the DELETE statement against your dbo.Cart table, and it captures what rows have been deleted; those are then in turn inserted into the dbo.Checkout table.
Also - that condition in the WHERE clause seems quite funny - basically, this will select and thus delete all rows from your table......
Update: I'm assuming you somehow store the relevant member's ID in #MemberID and use that to select the rows to delete from the dbo.Cart table..
Store your passing data to a newly declared variable and use that variable to everywhere as below, this will work fine, thanks
DECLARE #PassedMemberID INT
SET #PassedMemberID = MemberID --Store your passing MemberID here for later use
INSERT INTO Checkout(ProductID, MemberID, Quantity)
SELECT ProductID, MemberID, Quantity
FROM Cart
WHERE (MemberID = #PassedMemberID)
DELETE FROM CART WHERE (MemberID = #PassedMemberID)

Need help to transfer data from one table to another table in the same database

I'm using visual studio 2008 express running C#. I got Cart table and Checkout table in the same database which is an access file. both table have 3 same column which is ProductID,MemberID and Quantity.
So when I click a button in the Shopping cart page, the data in Cart table are supposed to transfer to Checkout table and the data in Cart are supposed to be cleared (which I haven't try yet because the transfer part is not successful). In the configure data source, SQL statement Insert part I wrote:
INSERT INTO [Checkout]
([ProductID], [MemberID], [Quantity])
VALUES (?, ?, ?)
SELECT([ProductID], [MemberID], [Quantity])
FROM [Cart]
WHERE ([MemberID]=?)
and the error is (Missing semicolon (;) at end of SQL statement.) come out at the aspx.cs (adsCart.Insert(); ) when I run it. When I try to execute query in configure datasource , the error is (Unable to parse query text.)
(I have also tried another way where the code is
string MemberID = User.Identity.Name;
string Product = ??? ;
string Quantity =??? ; (Both ??? is the part where I do not know what to put in.)
adsCart1.SelectParameters["MemberID"].DefaultValue = MemberID;
adsCart1.SelectParameters["ProductID"].DefaultValue = ProductID;
adsCart1.SelectParameters["Quantity"].DefaultValue = Quantity;
adsCheckout.InsertParameters["ProductID"].DefaultValue = ProductID;
adsCheckout.InsertParameters["MemberID"].DefaultValue = MemberID;
adsCheckout.InsertParameters["Quantity"].DefaultValue = Quantity;
adsCheckout.Insert();
But I do not know what to put in for the 2 ??? part so I only managed to transfer the memberID to the Checkout Table.)
Sorry if the Question is too long. Please help me to solve this problem or tell me the better way to transfer data from one table to another table. I also need help in creating simple search engine for products for my website.
INSERT INTO [Checkout] ([ProductID], [MemberID], [Quantity])
SELECT [ProductID], [MemberID], [Quantity]
FROM [Cart]
WHERE [MemberID]=?
Use SQL are per your first thoughts. The above statement should get the values you require. All you need to do is specify a member ID in the Where clause.
More information :
http://msdn.microsoft.com/en-us/library/ms188263.aspx
http://msdn.microsoft.com/en-us/library/ms189872.aspx