Passing New Variables to SQL

Results 1 to 2 of 2

Thread: Passing New Variables to SQL

  1. #1
    Join Date
    Dec 1969

    Default Passing New Variables to SQL

    This is probably a stupid question, but bear with me (I&#039m new to this stuff)...<BR><BR>I&#039ve setup a form that accepts user input and dumps it into a database when they click the submit button. At that point, I want to redirect the user to a form that displays the information that they&#039ve just entered after it&#039s been run through a bit of code. In order to do that, I need to pass the user&#039s email address as the criteria in my SQL statement. What do I need to do in order to capture the contents of the field that holds the email address, submit the rest of the form to the database, and then immediately open another page using the captured information in my sql? I know this can be done...just don&#039t know how.

  2. #2
    Garth Guest

    Default RE: Passing New Variables to SQL

    I am not 100% sure I understand what you are trying to accomplish, but I will try and help. I am assuming the INSERT process manipulates the data and you simply need to get the post-INSERTed data. If this is the case simply return the primary key (PK) for the record back to the client and re-query. I would use an IDENTITY field to make the PK part easier and a stored procedure to encapsulate the required code.<BR><BR>The following code shows you how the SQL-part works.<BR><BR>CREATE TABLE PKTest (pk smallint identity, f1 char(1))<BR>go<BR>CREATE PROCEDURE ps_PKTest<BR>@f1 char(1)<BR>AS<BR>INSERT PKTest VALUES(@f1)<BR>SELECT @@IDENTITY<BR>go<BR><BR>EXEC ps_PKTest &#039a&#039<BR><BR>When the sp is executed a new record is created and the PK is returned to the client. This may look a little confusing, but the @@IDENTITY part is the key. SQL Server keeps track of the last IDENTITY value INSERTed into the table with @@IDENTITY, so returning this value is a simple way to get the PK. The client can then execute a SELECT with the PK to get the post-INSERTed data.<BR><BR>Garth<BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts