|
Oh, I guess I misunderstood. Sorry.
SELECT c.dt, c.dayname, c.monthname, c.M, c.D, c.Y, c.W
FROM dbo.Calendar c,
(select top 1 dt + 14 as dt
from calendar c3
where dayname = 'Sunday' and dt >= '01/08/08' order by dt) as c2
WHERE c.dt >= c2.dt
AND c.Y IN (2008, 2009)
AND datediff(dd,c2.dt,c.dt ) % 21 = 0
ORDER BY c.dt
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
Not sure why you would want to set up a table containing each date when the calculations are just as easy to write.
Have you considered adding a few more fields to your table:
1) DayNameIndexForMonth (i.e. 1st Sunday, 2nd Sunday, 3rd Sunday)
2) DayOfYear (i.e. This is the 74th day of the year)
3) Quarter (i.e. This day is in the 3rd Quarter)
This might make your queries a lot easier.
On the other hand, you can always calculate on the fly. With only 365 iterations per year, loops should execute quickly enough. Knowing that the 3rd 'Day Name' of every month will fall between the 15th and 21st day you can code it like this:
CREATE PROCEDURE RecurringDates_ThirdSunday
(
@Start DATETIME,
@End DATETIME
)
AS
DECLARE @curDt DATETIME
SET @curDt = @Start
DECLARE @t TABLE (GoodDate DATETIME NOT NULL)
WHILE @curDt <= @End
BEGIN
IF DATEPART(dw,@curDt) = 1
BEGIN
IF DATEPART(dd,@curDt) BETWEEN 15 AND 21
BEGIN
INSERT INTO @t (GoodDate) VALUES (@curDt)
END
END
SET @curDt = DATEADD(dd,1,@CurDt)
END
SELECT *
FROM @t
|
|
|
|
|
I guess he wants every third Sunday from the start date, not every third Sunday of the month. I had the same misunderstanding.
But, I like your method better than creating a table with every date between now and some arbitrary future date.
CREATE PROCEDURE RecurringDates_ThirdSunday
(
@Start DATETIME,
@End DATETIME
)
AS
DECLARE @curDt DATETIME
SET @curDt = @Start
DECLARE @t TABLE (GoodDate DATETIME NOT NULL)
WHILE DATEPART(dw,@curDt) != 1
BEGIN
SET @curDt = DATEADD(dd,1,@CurDt)
END
SET @curDt = DATEADD(dd,14,@CurDt)
WHILE @curDt <= @End
BEGIN
INSERT INTO @t (GoodDate) VALUES (@curDt)
SET @curDt = DATEADD(dd,21,@CurDt)
END
SELECT *
FROM @t
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
Is there anyway to take the whole condition part of WHERE clause as a parameter of a stored procedure ?
The result I want to gain is something that would be achieved by executing this stored procedure.Although this is wrong , I want to achieve the result.Is there anyway get this result ?
CREATE PROCEDURE sp_GetList
@condition varchar(50)
AS
SELECT * FROM Table_X WHERE @condition
-- modified at 5:53 Monday 11th September, 2006
|
|
|
|
|
Yor problem is not very clear what exactly you want? Do you want to fetch the recordset on basis of some condition, if so then that is where you can use "where" clause. Something like this:
select * from emp where empid = @EID
where @EID is the parameter to that stored procedure.
Best Regards,
Apurva Kaushal
|
|
|
|
|
No No no.
I want the condition to be like a variable for example this:
condition -> (column1='x' AND column2='y' OR column3='z')
I want the stored procedure to take the aformentioned condition as a varchar and use it in WHERE clause. To take the whole condition part of WHERE CLAUSE as parameter.
Is there any way
-- modified at 5:50 Monday 11th September, 2006
|
|
|
|
|
ya sure there is a way what you can do is create your where condition in front end, send that complete condition as string to the stored procedure. In stored procedure you take one variable(where the sql statement will be stored) and append the where string and then use sp_executesql to execute the complete string.
Best Regards,
Apurva Kaushal
|
|
|
|
|
Thank you very much for your help.Could you please show me some basic code ?
|
|
|
|
|
Here a sample code which you can use in your stored procedure taken from sqlserver help:
DECLARE @IntVariable INT<br />
DECLARE @SQLString NVARCHAR(500)<br />
DECLARE @ParmDefinition NVARCHAR(500)<br />
<br />
<br />
SET @SQLString =<br />
N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = @level'<br />
SET @ParmDefinition = N'@level tinyint'<br />
<br />
SET @IntVariable = 35<br />
EXECUTE sp_executesql @SQLString, @ParmDefinition,<br />
@level = @IntVariable<br />
<br />
SET @IntVariable = 32<br />
EXECUTE sp_executesql @SQLString, @ParmDefinition,<br />
@level = @IntVariable<br />
Hope this will serve the purpose.
Best Regards,
Apurva Kaushal
|
|
|
|
|
CREATE PROCEDURE sp_GetList
@condition varchar(50)
AS
exec('SELECT * FROM Table_X WHERE ' + @condition)
Be sure to look at this:
SQL Injection Attacks and Some Tips on How to Prevent Them[^]
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
Hi Everybody!
I am using VB6.0 & SQL Server 7.0
Following is my database Connection code
Dim db As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
Set db = New ADODB.Connection
db.Provider = "SQLOLEDB"
db.ConnectionString = "server=SUNANDA;uid=sa;pwd=ranjita1;database=crm"
db.Open
Set rs = New ADODB.Recordset
sql = "Select * from AccountHead"
Set rs.ActiveConnection = db
rs.CursorLocation = adUseClient
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
rs.Open sql, , , , adCmdText
I have placed navigation buttons on my form, but the Next & Previous buttons are not working properly.
When I first time clicks the Next button , the recordset goes to the next record but when I click it second time, the record doesn’t get changed.
When I press the previous button, the recordset points to the first record, instead of pointing to the previous record.
Here is my code for navigational buttons :
Public Sub MoveFields()
txtAcId.Text = rs("AcHeadID")
txtAcName.Text = rs("AcHeadName")
txtAcMobile.Text = rs("Mobile")
txtAcEmail.Text = rs("EmailID")
End Sub
Private Sub cmdFirst_Click()
rs.MoveFirst
MoveFields
cmdPrev.Enabled = False
cmdNext.Enabled = True
End Sub
Private Sub cmdLast_Click()
rs.MoveLast
MoveFields
cmdNext.Enabled = False
cmdPrev.Enabled = True
End Sub
Private Sub cmdNext_Click()
rs.MoveNext
If rs.EOF Then
rs.MoveLast
cmdNext.Enabled = False
End If
MoveFields
cmdPrev.Enabled = True
End Sub
Private Sub cmdRemove_Click()
rs.Delete
rs.MoveNext
If rs.EOF Then
rs.MoveLast
End If
End Sub
Private Sub cmdPrev_Click()
rs.MovePrevious
If rs.BOF Then
rs.MoveFirst
cmdPrev.Enabled = False
End If
MoveFields
cmdNext.Enabled = True
End Sub
Pls. tell me, where I am wrong.
Thanks
|
|
|
|
|
Hi
Dear Please first check where you write the code for recordset and connection.
Then try following code for your record movement
Next record
if not rs.eof then
rs.movenext()
....
end if
previous record
if not rs.bof then
rs.moveprevious()
.....
end if
If the problem is there then please post new message with you complete form code so I can sort your issue.
Regards
Tushar kothari
|
|
|
|
|
I have written the code for recordset and connection in sub main()
I am calling the procedure in each click event.
|
|
|
|
|
Hi,
I am using ASp.NET 1.1
Now when I call a stored procedure from code behind file and the stored procedure raises an error how to catch this error in code behind file
Thanks and Regards,
Uma
|
|
|
|
|
Hi
you can catch exception by following
Catch ex As SqlClient.SqlException
Tushar kothari
|
|
|
|
|
You can use output paramter, return value or as mentioned SQlexception. I prefer return value as this help me to use customize message from resource file,
|
|
|
|
|
:(Hi Everybody!
I am using VB6.0 & SQL Server 7.0
Following is my database Connection code
Dim db As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
Set db = New ADODB.Connection
db.Provider = "SQLOLEDB"
db.ConnectionString = "server=SUNANDA;uid=sa;pwd=ranjita1;database=crm"
db.Open
Set rs = New ADODB.Recordset
sql = "Select * from AccountHead"
Set rs.ActiveConnection = db
rs.CursorLocation = adUseClient
rs.CursorType = adOpenStatic
rs.LockType = adLockOptimistic
rs.Open sql, , , , adCmdText
I have placed buttons on the form to Add, Delete, Edit and Update the records of the table. But in the click event of the Update button, the recordset is not supporting the Edit method. The only method it is showing in (VBA) the dropdown list is EditMode.
How will I then Edit the records?
Pls. help me out.
Thankyou.
|
|
|
|
|
Hi
Dear please concentrate on your following code
rs.CursorType = adOpenStatic
As you had open the cursor in Static mode there is no edit permission for this record set.
You gave to open the recordset in non static mode.
Regards
Tushar kothari
|
|
|
|
|
Hi! Tushar
I have tried with
rs.CursorType=adOpenDynamic
But of no use.
Pls. suggest.
regards
ranjita
|
|
|
|
|
Hi
If you want just to update the record in recordset then you have to set the fileds values of rs then call rs.update
If this method is not wirking the please relpy back then I will try some odd solution for you.
Regards
Tushar kothari
|
|
|
|
|
I wants to edit the existing record and then update.
what u r suggesting that i am using in the click event of my save button.
regards
ranjita
|
|
|
|
|
Hi
This is the tested code so please use it
Private Sub Form_Load()
Dim db As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sql As String
Set db = New ADODB.Connection
db.Provider = "SQLOLEDB"
db.ConnectionString = "Provider=SQLOLEDB.1;Password=thk;Persist Security Info=True;User ID=thk;Initial Catalog=Dhanavantari;Data Source=TUSHAR"
db.Open
Set rs = New ADODB.Recordset
sql = "Select * from WardMaster"
Set rs.ActiveConnection = db
rs.CursorLocation = adUseServer
rs.CursorType = adOpenDynamic
rs.LockType = adLockPessimistic
rs.Open sql, , , , adCmdText
End Sub
on Save client button
rs.Fields(1) = trim(txtwardname.text)
rs.Fields(2) = val(txtrate.text)
rs.Update
So now you can edit the recordset and then update it
ADO is the connected type of recordset so you can update one record at a time.
If you have some other problem which i had not understand the please brief it
regards
Tushar kothari
|
|
|
|
|
i am working in vb.net2.0 version.
can u help me with code that how can i populate the datagridview in edit mode which will open in another form when one selects the row of a datagridview.
regards
Taniya Banerjee
|
|
|
|
|
Hi,
I have a stored procedure with 1 parameter.
The value that I pass to the parameter is a select statement.
The question is how do I execute this select statement that is passed as parameter that returns the rows?
Thanks and Regards,
Uma
|
|
|
|
|
Hi,
You can use sp_executesql to execute the sqlquery which is in a variable.
Best Regards,
Apurva Kaushal
|
|
|
|