Problem with IF statement in SQL

 Can someone please tell me what I'm doing wrong here:

 

 

CREATE PROCEDURE dbo.AddProductToEnquiry
	@SessionID int,
	@ProductID int
AS
	DECLARE @CountProductInEnquiry int
	
	SET @CountProductInEnquiry = (SELECT Count(*) FROM Enquiry_Items WHERE SessionID=@SessionID AND ProductID=@ProductID)
	
	IF @CountProductInEnquiry = 0 THEN
		
				INSERT INTO Enquiry_Items
				([SessionID], [ProductID], [Date])
			VALUES
				(@SessionID, @ProductID, GETDATE())

	END IF

  

 

Cheers,

Curt


Regards, Curt
0
CurtWRC
12/5/2008 4:11:20 PM
asp.net.object-datasource 16182 articles. 0 followers. Follow

4 Replies
466 Views

Similar Articles

[PageSpeed] 18

 Actually I think Ive fixed it. Can someone please confirm to me however, that the DELETE statement beneath the semi-colon will be executed regardless of whether the IF Statement is true?

Thanks

 

 

 

ALTER PROCEDURE dbo.AddProductToEnquiry
	@SessionID varchar(200),
	@ProductID int
AS
	DECLARE @CountProductInEnquiry int
	
	SET @CountProductInEnquiry = (SELECT Count(*) FROM Enquiry_Items WHERE SessionID=@SessionID AND ProductID=@ProductID)
	
	IF @CountProductInEnquiry = 0
		
				INSERT INTO Enquiry_Items
				([SessionID], [ProductID], [Date])
			VALUES
				(@SessionID, @ProductID, GETDATE())
				
	;
	
	DELETE FROM Enquiry_Items WHERE [Date]<DATEADD(d, -1, GETDATE())
  
Regards, Curt
0
CurtWRC
12/5/2008 4:17:27 PM

TRY THIS , REMOVE THEN  KEYWORD 

 

CREATE PROCEDURE dbo.AddProductToEnquiry
@SessionID int,
@ProductID int
AS
DECLARE
@CountProductInEnquiry int

SET
@CountProductInEnquiry = (SELECT Count(*) FROM Enquiry_Items WHERE SessionID=@SessionID AND ProductID=@ProductID)

IF @CountProductInEnquiry = 0 

   BEGIN

 INSERT INTO Enquiry_Items
([SessionID], [ProductID], [Date])
VALUES
(@SessionID, @ProductID, GETDATE())

END

 

MARK AS ANSWER IF IT HELPS YOU


Mark the post as answer if it helps you!

Ashish Agrawal
----------------------------
MCPD(.NET Framework 2.0,Web Developer)
MCTS(SQL Server 2005)
0
agrawalbs
12/5/2008 4:19:28 PM

Ya, it will be executed always,

for safety you can use BEGIN AND END to specify IF block


Mark the post as answer if it helps you!

Ashish Agrawal
----------------------------
MCPD(.NET Framework 2.0,Web Developer)
MCTS(SQL Server 2005)
0
agrawalbs
12/5/2008 4:22:45 PM

 Cheers :)


Regards, Curt
0
CurtWRC
12/5/2008 6:22:16 PM
Reply:

Similar Artilces:

Problem accessing SQL Stored Procedure From .Net using hibernate object
Hi, I am calling a SQL Stored procedure from .net application using Hibernate object, but somehow its telling me "The 'callable' attribute is not declared.", I am not sure what's wrong I have done here. Will really appreciate if someone can help me for this. Herewith I am providing my Stored Procedure, my .hbm file and code to call it. can someone pls tell me what's wrong in this? Also, if possible, pls tell me how to set result in a list object. Thanks in advance, ######################################### MY STORED PROCEDURE : ALTER PROCEDURE [dbo].[RulesDetails_...

Object Datasource problem with Business Layer Object
Hi,  I have created dataaccess layer using LINQ. I am using LINQ to stored procedure on context class . on Business Layer I define a partial class in which I define all functions to Select,Insert,Update and Delete. When I am binding BLL class to Object Datasource only Select function is showing in Object Data source . please provide me way to bind buisness class to object datasource Please remember to mark the replies as answers if they help .Jeetendra SharmaSoftware EngineerJodhpur (Raj.)India Make sure you properly notate your class and methods with the proper DataObject attribut...

SQL DataSource : Object Reference not set to an instance of an object
 Dear friendsI am getting this error from few days (it was not in past)when i add new "sqlDataSource" , I set the Server name, user name and password then i select the Database, when i click on "Test Connection" button, it does not give me any Error and it shows that Test connection Successful, but when i click "OK", It shows the Error box with the error "Object Reference not set to an instance of an object"From few days it is happening, I was seting the datasource without any problem in past.I am using SQL Server 2005Any one can help? aliusmankha...

Problem with analytic sql function (The OVER SQL construct or statement is not supported)
Hi All!  Could You comment the next situation:I'm configuring my TableAdapter just like Scott Mitchell does in his tutorialhttp://www.asp.net/learn/data-access/tutorial-70-vb.aspxThe only principal difference is that I need Insert/update and deletemethods to be generated (His aim is only SELECT).I'm also using analytic function (ROW_NUMBER) and I'm also gettinwarning “The OVER SQL construct or statement is not supported.” Yousay then that it could be ignored. But, in this case statements tomodify data (insert/update and delete) aren't being generated, thoughafter w...

Problems with SQL statement
Hello all I have the below SQL statement querying my access database. To me the code looks fine and I can't see what’s wrong with it, but when I execute it I get the syntax error message at the bottom of this post. The code is supposed to take input text from a search field and use it to search selected fields in the database. It then returns these where there are matches and where the quantity is = 1. I'm sure the problem is something really simple but all help would be appreciated. The SQL query: ("Select ProductID, ProductName, " _ & "BriefDescription, ...

SQL Statement Problem
Hello all, Let's see if we have some pro's out here or not. This could be simple actually, but I don't know how... I have two tables, "Projects", and "Tasks." Now what I am trying to do is list the projects on a page. And I have managed to do that nicely. And, while it's looking, I actually have it open up another recordset inside of that one (which is looping) and have it lookup the tasks status percentage. So, when looking at one record, it looks like this: Projects - ID Title Status 1 Sam...

Problem with sql statement
Hi all I've got some trouble with a sql statement. What I have got: 4 Tables 1. tFuel------------------------FuelId | FuelDescription ... 1 | Diesel  2 | SomeOtherKindOfFuel 2. tFuelPrice------------------------GasStationId | FuelId | Price ...1 | 1 | 1.451 | 2 | 1.30 3. tGasStation------------------------GasStationId | GasStationGroupId | Name ...1 | 2 | Millers Gas Station |2 | 1 | Chucky's Gas Station | 4. tGasStationGroup------------------------GasStationGroupId | Name ...1 | BP2 | Shell So, what I want to achive is the following I'd like to show the user a list of all...

A problem about SQL Statement
Hello, I've got a problem about a Select statement with the following criteria: Data Table Key Num1 Num2 A 1 2 A 1 2 A 2 3 B 0 0 B 1 1 C 2 1 C 1 2 I would like to select all row of a key if the (sum of num1 - sum of num2) with that key != 0 In the above case, the outcome would be: A 1 2 A 1 2 A 2 3 Could any body advise what the SQL should be? Many thanks!   Hi,newbie Try Like This I have shown the select query in bold   CReate table Test_Sql ([Key] varchar(20),[Num1] int, [Num2] i...

SQL Statement problem
I am trying to pull certain information from a database by using the ISQL window, but Powerbuilder keeps locking up when I type the following code, could someone please show me what I'm doing wrong (assuming that all of the spelling is correct)? Thanks!!! SELECT t0.code, t0.billcode FROM billcode t0 WHERE t0.code in (SELECT distinct t1.code FROM app_charges_old t1 WHERE t1.invoice_number IN (SELECT t2.invoice_number FROM invoice_summary_old t2 WHER...

SQL statement problems
Having problems with our SELECT statements in both Webforms and client/server application that I wasn't having in the various demo versions. I am using "SNC" (SQL Native Client installed) connection. Connected to DB with transaction object called "SQLCA2". This code for debugging purposes works: select count(*) into :ll from users using SQLCA2; This actual code does NOT work: SELECT count (*) INTO :l_i_count FROM users WHERE user_id = :a_s_user_id using sqlca2; From debugger: sqlcode=0 sqldbcode=0 sqlerrtext="" Any ideas would b...

SQL-Statement problem
Dear all i am having a problem with SQL-Statement-1.005, whereas SQL-Statement-0.1020 is working fine. SQL ERROR: Mismatched parentheses in WHERE clause! SV = RV(0x810cad0) at 0x829105c REFCNT = 1 FLAGS = (PADBUSY,PADMY,ROK) RV = 0x83844e4 DBD::CSV::st execute failed: dbih_getcom handle 'DBD::CSV::Statement=HASH(0x83844e4)' is not a DBI handle (has no magic) at /usr/local/lib/perl5/site_perl/5.6.0/SQL/Statement.pm line 164. I think i tracked this down to Parser.pm the cleansql routine which skips closing parens, but not the corresponding openings: wa...

SQL::Statement problem
I posted a similar message to Jeff directly but I never got a confirmation that he actually received. So let's try here... Firstly, I've found out that RLIKE is broken: sub is_matched { ... if ($op =~ /LIKE|CLIKE/i) { $val2 = quotemeta($val2); $val2 =~ s/\\%/.*/g; $val2 =~ s/_/./g; } If $op i s'RLIKE', and since 'RLIKE' contains the string 'LIKE', the pattern is run through quotemeta(). Also, the SQL operators IS and the LIKE family are case-sensitive, which is contrary to SQL specs if I'm not mista...

SQL statement problems
Hey gang, I'm trying to work out why on earth my SQL statement refuses to work. I've tested it on my db within Access, yet working off the website itself, it just refuses to return anything when there should be 7 or so listed. Can anyone see anything wrong with my statement (warning, it's long): strConnection = @"Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + (string) Server.MapPath("GAAP.mdb"); strSqlCommand = "SELECT STUDENT.STUDENT_ID, STUDENT.STUDENT_LAST_NAME, STUDENT.STUDENT_FIRST_NAME, UNIT_STUDENT.STUDENT_EMAIL, UNIT_STUDENT.STUDENT_PASSWO...

SQL Statement problem
Can someone please tell me what is wrong with my SQL below..I think I am missing a quote somewhere... "select distinct itype from TR_data_and_history where stationname ='" & StationNameString & " and highkv ='" & VoltageString & "' " StationNameString single quotes are not endedSushila Bowalekar PatelVisual ASP/ASP.NET MVPhttp://weblogs.asp.net/sushilasb Thank you..that was the problem. Final statement = "select distinct itype from TR_data_and_history where stationname ='" & StationNameString & "' and highkv...

Web resources about - Problem with IF statement in SQL - asp.net.object-datasource

User:Jimbo Wales/Statement of principles - Wikipedia, the free encyclopedia
As we move forward with software and social changes, I think it is imperative that I state clearly and forcefully my views on openness and the ...

Category:Articles containing potentially dated statements from June 2006 - Wikipedia, the free encyclopedia ...
This is an administration category . It is used for administration of the Wikipedia project and is not part of the encyclopedia. It contains ...

David Bowie: Private ceremony planned as family issues statement thanking fans
LONDON, Jan 14 Reuters - David Bowie will be remembered in a private ceremony, says his family, who have thanked fans for their support after ...

Foxborough issues statement on handling of reported Chandler Jones incident
The statement, signed by Foxborough’s town manager, did not provide any new details about the case.

Is Mark Zuckerberg’s latest post about his daughter a political statement?
... doctor. His captioned it, “ Doctor’s visit — time for vaccines! ” and the post went viral (pun possibly intended.) Seen as an official statement ...

Chipotle sued over food safety statements
Chipotle Mexican Grill was sued for allegedly misleading investors about its food safety controls.

Tesla Drivers Do ‘Crazy Things’ While Autopilot Is Engaged, Musk Issues Statement
... within two years, the Tesla will be able to be "summoned," like Lassie, from miles away.

Business on Display: Making a Statement with Digital Signage
The digital signage market is projected to be worth $22 billion by 2020. Join us for our first of four webinars powered by Samsung as we discuss ...

'Affluenza' Teen's Mom's Lawyers Issue Sarcastic Statement to Sheriff
Couch's lawyers asked that proceedings to be conducted "fairly and lawfully."

Make a style statement with Cinderella’s glass slippers
Perspex heels, which draw inspiration from the fairy tale’s classic shoe style, are in vogue now

Resources last updated: 1/19/2016 11:41:08 AM