Accessing a field in SQL

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 27
Reputation: Pankaj18 is an unknown quantity at this point 
Solved Threads: 0
Pankaj18 Pankaj18 is offline Offline
Light Poster

Accessing a field in SQL

 
-1
  #1
Aug 3rd, 2009
Hi,

We have schedule a job through SQL agent

in which

We want to access the data of a specific field from SQL table

For example we have a field named interviewDate, we have to compare it with current date.

now we have to write a stored procedure to read the date field.

This is incorrect syntax which we tried(doesn't work)

  1. CREATE PROCEDURE chkinterview
  2. as
  3. SELECT * FROM interview
  4. if(interviewdate!='2/7/1900 12:00:00 AM')
  5. print'ok'
  6. ELSE
  7. print'not ok'



what will be the syntax to access all the record of interviewdate field.

Later we have to insert that data into another table through a stored procedure.

Thanks !

Pankaj Singh
Last edited by peter_budo; Aug 3rd, 2009 at 11:55 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,254
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 579
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Accessing a field in SQL

 
0
  #2
Aug 3rd, 2009
First: use code tags when posting your code:

[code=sql]
...query here
[/code]


Secondly: This is an MSSQL question, not an ASP.NET question. The problem here is your query syntax.

Third: The "not equal" operation in MSSQL is <> and not !=. You also need to select one record from your interview table to compare against .. unless you're trying to compare all dates? Please explain what you are doing. Next with DateTimes and interviews you will likely want to have a Start/End date unless all interviews are of a fixed duration. Please clarify.

  1. CREATE PROCEDURE chkinterview
  2. As
  3. BEGIN
  4.  
  5. DECLARE @someValue INT
  6. IF ((SELECT interviewdate FROM interview WHERE someColumn = @someValue) <> Cast('2/7/1900 12:00:00 AM' as DATETIME))
  7. PRINT 'OK'
  8. ELSE
  9. PRINT 'NOT OK'
  10.  
  11. END
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 27
Reputation: Pankaj18 is an unknown quantity at this point 
Solved Threads: 0
Pankaj18 Pankaj18 is offline Offline
Light Poster

Re: Accessing a field in SQL

 
0
  #3
Aug 3rd, 2009
This works for a single row but what if there are multiple rows(6000). Do we have a loop for that.
Then
We have to copy the whole row into another table.
Can you please explain on this as well.
Thanks
Pankaj Singh
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,254
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 579
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Accessing a field in SQL

 
0
  #4
Aug 3rd, 2009
>> This works for a single row but what if there are multiple rows(6000).
I'm aware and stated that limitation in my original post.

  1. CREATE PROCEDURE chkinterview
  2. As
  3. BEGIN
  4.  
  5. DECLARE @someValue INT
  6. IF EXISTS(SELECT * FROM Interview WHERE InterviewDate = Cast('2/7/1900 12:00:00 AM' as DATETIME))
  7. PRINT 'NOT OK'
  8. ELSE
  9. PRINT 'OK'
  10.  
  11. END
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 27
Reputation: Pankaj18 is an unknown quantity at this point 
Solved Threads: 0
Pankaj18 Pankaj18 is offline Offline
Light Poster

Re: Accessing a field in SQL

 
0
  #5
Aug 4th, 2009
Hi,

  1. CREATE Procedure chkinterview
  2. AS
  3. BEGIN
  4. Declare @someValue int
  5. IF EXISTS(SELECT * FROM Interview WHERE InterviewDate = Cast('2/7/1900 12:00:00 AM' AS datetime))
  6. PRINT 'NOT OK'
  7. ELSE
  8. PRINT 'OK'
  9. END
In above code:
If the Condition is true ,so we have to copy the selected column(expr1,expr2,expr3..) into another table.
Can you please explain on this as well.
Thanks
Pankaj Singh
Last edited by Ezzaral; Aug 4th, 2009 at 3:37 pm. Reason: Added [code] [/code] tags. Please use them to format any code that you post.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,254
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 579
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Accessing a field in SQL

 
0
  #6
Aug 4th, 2009
Please use code tags when posting on DaniWeb!

For your question -- Not really. I don't think I understand what you are asking.

"In the above code: If the condition is true" -- means that if the appointment already exists you want to copy it to another table?
  1. IF EXISTS(SELECT * FROM Interview WHERE InterviewDate = Cast('2/7/1900 12:00:00 AM' AS DATETIME))
  2. BEGIN
  3. INSERT INTO otherTable (col1,col2,col3)
  4. SELECT col1,col2,col3
  5. FROM INTERVAL
  6. WHERE InterviewDate = Cast('2/7/1900 12:00:00 AM' AS DATETIME)
  7. END
Last edited by sknake; Aug 4th, 2009 at 10:07 am.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC