473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get values from queries, and then using in another query?

Hi

I have the following tables and stored procedure. I need to pass a value to
the stored procedure and have it use the value in a query. After running
that query it will return an ID which is then used in an insert statement.
At present the values in @MovieId int, @UserID int are left empty (see my
original code at the bottom of posting). I think this is due to an issue
with when the sql is executed (?).

I get the feeling I should have something like this instead, but not sure:

ALTER proc inserttransactions

@MovieName nvarchar(50) = 'team',
@uName nvarchar(50) = 'frank',
@FrameNumber int = 0

as

DECLARE @MovieId int, @UserID int

-- Find MovieID from MovieName
@MovieId = exec MovieName2Id @MovieName

-- Find UserID from UserEmail
@UserEmail = exec Email2UserId @uName

-- Insert Data
INSERT INTO Transactions(MovieId, UserId, FrameNumber)
VALUES (@MovieId, @UserID, @FrameNumber)
Thanks in advance.


========MY CODE=============
Tables:

CREATE TABLE [dbo].[movies] (
[movieID] [int] IDENTITY (1, 1) NOT NULL ,
[movieName] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[movieDateAdded] [datetime] NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[transactions] (
[userID] [int] NULL ,
[movieID] [int] NULL ,
[FrameNumber] [int] NOT NULL ,
[transDate] [datetime] NOT NULL
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[users] (
[userID] [int] IDENTITY (1, 1) NOT NULL ,
[userEmail] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[userDateRegistered] [datetime] NULL
) ON [PRIMARY]
GO

Stored Procedure:

ALTER proc inserttransactions

@MovieName nvarchar(50) = 'team',
@uName nvarchar(50) = 'frank',
@FrameNumber int = 0

as

DECLARE @MovieId int, @UserID int

-- Find MovieID from MovieName
SELECT @MovieId = MovieId
FROM Movies
WHERE MovieName = @MovieName

-- Find UserID from UserEmail
SELECT @UserID = UserID
FROM Users
WHERE UserEmail = @uName

-- Insert Data
INSERT INTO Transactions(MovieId, UserId, FrameNumber)
VALUES (@MovieId, @UserID, @FrameNumber)

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO


Jul 20 '05 #1
2 2111
You don't return values from EXEC like that. You need to specify an OUTPUT
parameter. See Books Online for examples.

Let me make some assumptions about your SPs and your DDL. I would re-write
your code like this:

CREATE PROCEDURE inserttransactions
(@MovieName NVARCHAR(50) = 'team', @uName NVARCHAR(50) = 'frank',
@FrameNumber INTEGER = 0)
AS

INSERT INTO Transactions (movieid, userid, framenumber)
SELECT
(SELECT movieid
FROM Movies
WHERE moviename = @MovieName),
(SELECT userid
FROM Users
WHERE username = @uName),
@FrameNumber

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2
>> I have the following tables and stored procedure. I need to pass a
value to the stored procedure and have it use the value in a query.
After running that query it will return an id which is then used in an
insert statement. <<

You have some serious schema problems. There are no keys on these
"tables" -- without a key, they are not tables and you know that
IDENTITY is **never** a key by definition. The USPS says a name line
on an address label is CHAR(35), so why are you allowing CHAR(50)?

Can you use the UPC code for the movie's natural key? Did you research
the movie industry to see if they have another code?

How can you allow a transaction without having a user or a movie
involved, yet have it exist at a point in time? Why did you allow an
NVARCHAR(n) email address -- do you want to have a lot of non-Latin
garbage in the database?

You have a natural and verifiable key for a user -- his email address,
so use it.

CREATE TABLE Users
(user_email VARCHAR(128) NOT NULL -- what is the max length?
CHECK (<< valid email >>)
user_name CHAR(35) NOT NULL, --USPS standard
userdate_registered DATETIME
DEFAULT CURRENT_TIMESTAMP NOT NULL);

CREATE TABLE Movies
(movie_id CHAR(10) NOT NULL PRIMARY KEY, --upc code
movie_name NVARCHAR (50) NOT NULL, -- foreign films allowed
movie_date_added DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL);

CREATE TABLE Transactions
(user_email VARCHAR(50) NOT NULL
REFERENCES Users(user_email)
ON UPDATE CASCADE,
movie_id CHAR(10) NOT NULL
REFERENCES Users(user_email)
ON UPDATE CASCADE,
frame_number INTEGER NOT NULL,
trans_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL);

-- do it in one simple, pure SQL statement

CREATE PROCEDURE InsertTransactions
(@my_movie_name NVARCHAR(50),
@my_user_name VARCHAR(35))
AS
BEGIN
INSERT INTO Transactions (movie_id, user_email, frame_number)
SELECT movie_id, user_email, @frame_number
FROM Movies AS M1, Users AS U1
WHERE M1.movie_name = @my_movie_name
AND U1.user_name = @my_user_name;

<<error handling goes here>>;
END;

Your real problem is conceptual; you are still writing procedural code
in a non-procedural language. You break things down into steps and do
them one at a time -- "find movie_id" and then "find user_id" and then
"build a row" and then "insert the new row". SQL does not work that
way; you want to think "insert a new row, which has the following
attributes and values" as one unit of work.
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John | last post by:
I've read several prior posts in this group about using nz() to convert null values to zero; however, I'm not sure how/where to implement this function in my crosstab query. The crosstab query...
5
by: Daniel Tan | last post by:
Are there anyway to copy rows of records from one query to another query and then hide the records in source query ? Pls advise. Thanks. Regards, Daniel
0
by: Paweł Wróbel | last post by:
Hello! My situation looks like this: I have a table with columns: id_user, id_product, amount I have a form, on which I can choose usera, product and input amount. And the problem: After I...
3
by: milam | last post by:
Greetings, I would like to use a query result as a column name in another query, I can't seem to get this to work using Subqueries. Is there a good way to do this? Example: Table...
7
by: Daz | last post by:
Hi. I am trying to select data from two separate MySQL tables, where I cannot use join, but when I put the two select queries into a single query, I get an error telling me to check my syntax. Both...
5
by: listerofsmeg01 | last post by:
Hi, Pretty new to PHP and MySQL. I have a page on my site that displays a lot of information from various tables. Currently I have lots of small PHP wrapper functions around SQL queries to...
4
by: blake15 | last post by:
Hey, I'm desperate for a clue in on how ms access can do this: I'd like take the maximum value from one field and then populate another field by dividing the values of the first field by the max. ...
3
accessbunnie
by: accessbunnie | last post by:
Hello Access Users! I am a bit of an Access novice and am currently creating a database and have come up against a huge (for me!) road block. I do not write in Access code and instead, tend to...
2
by: iheartvba | last post by:
Hi, I usually use access query builder to create complex queries. Especially to create Sub Queries I.E. a querying another query. In my current situation I need to provide the criteria to the query...
31
by: WannabePrgmr | last post by:
I am working in Access 2003 and have approximately 10 fields that I want to be able to query on. The catch is that if I only fill in 3 or 4 fields, only the data that matches only those three fields...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.