Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Monday, March 12, 2012

past and future date

How should I find the dates for 3 days past and 5 days future. Such as TodayDate-3 and TodayDate+5 base in the date only, discarding the time?

Hi Jim,

You would have to use DATEADD function in your SQL sentence.

SELECT current, DATEADD( day, -3, current ) AS past, DATEADD( day, 5, current ) AS future
FROM t_table;

If you want use to today. You would replace 'current' column by GETDATE() function.

Good Coding!

Javier Luna
http://guydotnetxmlwebservices.blogspot.com/

|||

Also, to get rid of time you can do something like this:

select cast(convert(varchar(10),getdate(),112) as datetime)

|||

JIM.H. wrote:

How should I find the dates for 3 days past and 5 days future. Such as TodayDate-3 and TodayDate+5 base in the date only, discarding the time?

SELECT convert(varchar(15), DATEADD( day, -3, GETDATE()), 101) AS [3 Days Ago], convert(varchar(15), DATEADD( day, 5, GETDATE()), 101) AS [5 days from now]

Adamus

|||

Nope,

Anamus, this is my code :)

Good Coding!

Javier Luna
http://guydotnetxmlwebservices.blogspot.com/

|||

This is likely to be a bit more efficient, since it doesn't

require converting from datetime to varchar:

SELECT

dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],

dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]

Steve Kass

Drew University

www.stevekass.com

JIM.H.@.discussions.microsoft.com wrote:

> How should I find the dates for 3 days past and 5 days future. Such as

> TodayDate-3 and TodayDate+5 base in the date only, discarding the time?

>

>

|||

NNTP User wrote:

This is likely to be a bit more efficient, since it doesn't

require converting from datetime to varchar:

SELECT

dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],

dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]

Steve Kass

Drew University

www.stevekass.com

JIM.H.@.discussions.microsoft.com wrote:

> How should I find the dates for 3 days past and 5 days future. Such as

> TodayDate-3 and TodayDate+5 base in the date only, discarding the time?

>

>

This incorporates the time. The requestor does not want the time.

Adamus

|||

Adamus Turner@.discussions.microsoft.com wrote:

> [quote user="NNTP User"]

>

> This is likely to be a bit more efficient, since it doesn't

> require converting from datetime to varchar:

>

>

> SELECT

> dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],

> dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]

>

> Steve Kass

> Drew University

> www.stevekass.com

>

> JIM.H.@.discussions.microsoft.com wrote:

>

>

>>How should I find the dates for 3 days past and 5 days future. Such as

>>TodayDate-3 and TodayDate+5 base in the date only, discarding the

>

> time?

>

>>

>

>

> [/quote]This incorporates the time. The requestor does not want the

> time.

>

> Adamus

>

>

This does not incorporate the time. Did you test it?

SK

|||

Yes...the time is 00:00:00:0000

Bad format

Did you test it?

Adamus

|||Yes, I tested it.

The query I posted was

SELECT
dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],
dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]

The result in SQL Server (Query Analyzer, standard installation) is

3 Days Ago 5 Days from now
- -
2006-09-01 00:00:00.000 2006-09-09 00:00:00.000

Perhaps you think that the OP did not want the 00:00:00.000 to appear. Perhaps. Jim wanted to "find the dates," "discarding the time." There is no DATE data type in SQL Server, so I thought the best solution was to find the dates with midnight as the time. That's commonly referred to as a datetime without a time part. I don't think it's a good idea to select a string. The result set of the query you suggested, for example, would be interpreted as January 9 and September 9 in some cultures, and as September 1 and September 9 in others. My results would be presented and interpreted correctly regardless of culture.

SK|||Are you also NNTPUser?|||Thanks Steve. I am using this.|||

Steve Kass wrote:

Yes, I tested it.
The query I posted was
SELECT
dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],
dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]
The result in SQL Server (Query Analyzer, standard installation) is
3 Days Ago 5 Days from now
- -
2006-09-01 00:00:00.000 2006-09-09 00:00:00.000
Perhaps you think that the OP did not want the 00:00:00.000 to appear. Perhaps. Jim wanted to "find the dates," "discarding the time." There is no DATE data type in SQL Server, so I thought the best solution was to find the dates with midnight as the time. That's commonly referred to as a datetime without a time part. I don't think it's a good idea to select a string. The result set of the query you suggested, for example, would be interpreted as January 9 and September 9 in some cultures, and as September 1 and September 9 in others. My results would be presented and interpreted correctly regardless of culture.
SK

The comment on culture is incorrect. Converting the date to a string will not change the compiler interpretation of midnight. In fact, it will set the time as midnight. At no time, and under no circumstance will the date be incorrect.

As a side note: It is very poor programming practice to misuse primitive functions such as datediff() as some form of clever workaround to help justify an inability to correctly resolve an issue.

In good programming ethics and to avoid maintenace confusion, this elementary trickery should be avoided at all costs.

Adamus

|||

Steve Kass wrote:

Yes, I tested it.

The query I posted was

SELECT
dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],
dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]

The result in SQL Server (Query Analyzer, standard installation) is

3 Days Ago 5 Days from now
- -
2006-09-01 00:00:00.000 2006-09-09 00:00:00.000

Perhaps you think that the OP did not want the 00:00:00.000 to appear. Perhaps. Jim wanted to "find the dates," "discarding the time." There is no DATE data type in SQL Server, so I thought the best solution was to find the dates with midnight as the time. That's commonly referred to as a datetime without a time part. I don't think it's a good idea to select a string. The result set of the query you suggested, for example, would be interpreted as January 9 and September 9 in some cultures, and as September 1 and September 9 in others. My results would be presented and interpreted correctly regardless of culture.

SK

Perhaps English is your second or third language:

"How should I find the dates for 3 days past and 5 days future. Such as TodayDate-3 and TodayDate+5 base in the date only, discarding the time?"

|||

Adamus Turner@.discussions.microsoft.com wrote:

> This post has been edited either by the author or a moderator in the

> Microsoft Forums: http://forums.microsoft.com

>

> [quote user="Steve Kass"]Yes, I tested it.

>

> The query I posted was

>

> SELECT

> dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],

> dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]

>

> The result in SQL Server (Query Analyzer, standard installation) is

>

> 3 Days Ago 5 Days from now

> - -

> 2006-09-01 00:00:00.000 2006-09-09 00:00:00.000

>

> Perhaps you think that the OP did not want the 00:00:00.000 to appear.

> Perhaps. Jim wanted to "find the dates," "discarding the time." There is

> no DATE data type in SQL Server, so I thought the best solution was to

> find the dates with midnight as the time. That's commonly referred to as

> a datetime without a time part. I don't think it's a good idea to select

> a string. The result set of the query you suggested, for example, would

> be interpreted as January 9 and September 9 in some cultures, and as

> September 1 and September 9 in others. My results would be presented and

> interpreted correctly regardless of culture.

>

> SK

> [/quote]The comment on culture is incorrect. Converting the date to a

> string will not change the compiler interpretation of midnight. In fact,

> it will set the time as midnight. At no time, and under no circumstance

> will the date be incorrect.

I never said the time portion might be interpreted incorrectly.

But you are simply wrong about the date. The string results of your

code will represent wrong dates depending on the culture. Here is a

repro using the code you posted to show that:

set language French

go

create table AdamusDates (

[3 Days Ago] datetime,

[5 days from now] datetime

)

insert into AdamusDates

SELECT

convert(varchar(15), DATEADD( day, -3, GETDATE()), 101) AS [3 Days Ago],

convert(varchar(15), DATEADD( day, 5, GETDATE()), 101) AS [5 days from now]

set language English

insert into AdamusDates

SELECT

convert(varchar(15), DATEADD( day, -3, GETDATE()), 101) AS [3 Days Ago],

convert(varchar(15), DATEADD( day, 5, GETDATE()), 101) AS [5 days from now]

go

select * from AdamusDates

go

drop table AdamusDates

Results:

3 Days Ago 5 days from now

-- --

2006-02-09 00:00:00.000 2006-10-09 00:00:00.000

2006-09-02 00:00:00.000 2006-09-10 00:00:00.000

>

> As a side note: It is very poor programming practice to misuse primitive

> functions such as dateadd() as some form of clever workaround to help

> justify an inability to correctly resolve an issue. DateDiff()?

Dateadd() and Datediff() are well-documented and entirely appropriate

to a question about dates. Apparently you would prefer to maintain your

inability to correctly resolve the issue, as some form of obstinate

resistance to the use of language elements that would solve the problem.

>

> In good programming ethics and to avoid maintenace confusion, this

> elementary trickery should be avoided at all costs.

It's not trickery. It's a simple application of documented behavior

of T-SQL. And it works in the US, in France, and elsewhere.

SK

>

> Adamus

>

>

past and future date

How should I find the dates for 3 days past and 5 days future. Such as TodayDate-3 and TodayDate+5 base in the date only, discarding the time?

Hi Jim,

You would have to use DATEADD function in your SQL sentence.

SELECT current, DATEADD( day, -3, current ) AS past, DATEADD( day, 5, current ) AS future
FROM t_table;

If you want use to today. You would replace 'current' column by GETDATE() function.

Good Coding!

Javier Luna
http://guydotnetxmlwebservices.blogspot.com/

|||

Also, to get rid of time you can do something like this:

select cast(convert(varchar(10),getdate(),112) as datetime)

|||

JIM.H. wrote:

How should I find the dates for 3 days past and 5 days future. Such as TodayDate-3 and TodayDate+5 base in the date only, discarding the time?

SELECT convert(varchar(15), DATEADD( day, -3, GETDATE()), 101) AS [3 Days Ago], convert(varchar(15), DATEADD( day, 5, GETDATE()), 101) AS [5 days from now]

Adamus

|||

Nope,

Anamus, this is my code :)

Good Coding!

Javier Luna
http://guydotnetxmlwebservices.blogspot.com/

|||

This is likely to be a bit more efficient, since it doesn't require converting from datetime to varchar: SELECT dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago], dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now] Steve Kass Drew University www.stevekass.com JIM.H.@.discussions.microsoft.com wrote:
> How should I find the dates for 3 days past and 5 days future. Such as
> TodayDate-3 and TodayDate+5 base in the date only, discarding the time?
>
>

|||

NNTP User wrote:

This is likely to be a bit more efficient, since it doesn't require converting from datetime to varchar: SELECT dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago], dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now] Steve Kass Drew University www.stevekass.com JIM.H.@.discussions.microsoft.com wrote: > How should I find the dates for 3 days past and 5 days future. Such as > TodayDate-3 and TodayDate+5 base in the date only, discarding the time? > >

This incorporates the time. The requestor does not want the time.

Adamus

|||

Adamus Turner@.discussions.microsoft.com wrote:
> [quote user="NNTP User"]
>
> This is likely to be a bit more efficient, since it doesn't
> require converting from datetime to varchar:
>
>
> SELECT
> dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],
> dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]
>
> Steve Kass
> Drew University
> www.stevekass.com
>
> JIM.H.@.discussions.microsoft.com wrote:
>
> >>How should I find the dates for 3 days past and 5 days future. Such as >>TodayDate-3 and TodayDate+5 base in the date only, discarding the
>
> time?
> >>
>
>
> [/quote]This incorporates the time. The requestor does not want the
> time.
>
> Adamus
>
> This does not incorporate the time. Did you test it? SK

|||

Yes...the time is 00:00:00:0000

Bad format

Did you test it?

Adamus

|||Yes, I tested it.

The query I posted was

SELECT
dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],
dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]

The result in SQL Server (Query Analyzer, standard installation) is

3 Days Ago 5 Days from now
- -
2006-09-01 00:00:00.000 2006-09-09 00:00:00.000

Perhaps you think that the OP did not want the 00:00:00.000 to appear. Perhaps. Jim wanted to "find the dates," "discarding the time." There is no DATE data type in SQL Server, so I thought the best solution was to find the dates with midnight as the time. That's commonly referred to as a datetime without a time part. I don't think it's a good idea to select a string. The result set of the query you suggested, for example, would be interpreted as January 9 and September 9 in some cultures, and as September 1 and September 9 in others. My results would be presented and interpreted correctly regardless of culture.

SK
|||Are you also NNTPUser?|||Thanks Steve. I am using this.|||

Steve Kass wrote:

Yes, I tested it.
The query I posted was
SELECT
dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],
dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]
The result in SQL Server (Query Analyzer, standard installation) is
3 Days Ago 5 Days from now
- -
2006-09-01 00:00:00.000 2006-09-09 00:00:00.000
Perhaps you think that the OP did not want the 00:00:00.000 to appear. Perhaps. Jim wanted to "find the dates," "discarding the time." There is no DATE data type in SQL Server, so I thought the best solution was to find the dates with midnight as the time. That's commonly referred to as a datetime without a time part. I don't think it's a good idea to select a string. The result set of the query you suggested, for example, would be interpreted as January 9 and September 9 in some cultures, and as September 1 and September 9 in others. My results would be presented and interpreted correctly regardless of culture.
SK

The comment on culture is incorrect. Converting the date to a string will not change the compiler interpretation of midnight. In fact, it will set the time as midnight. At no time, and under no circumstance will the date be incorrect.

As a side note: It is very poor programming practice to misuse primitive functions such as datediff() as some form of clever workaround to help justify an inability to correctly resolve an issue.

In good programming ethics and to avoid maintenace confusion, this elementary trickery should be avoided at all costs.

Adamus

|||

Steve Kass wrote:

Yes, I tested it.

The query I posted was

SELECT
dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],
dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]

The result in SQL Server (Query Analyzer, standard installation) is

3 Days Ago 5 Days from now
- -
2006-09-01 00:00:00.000 2006-09-09 00:00:00.000

Perhaps you think that the OP did not want the 00:00:00.000 to appear. Perhaps. Jim wanted to "find the dates," "discarding the time." There is no DATE data type in SQL Server, so I thought the best solution was to find the dates with midnight as the time. That's commonly referred to as a datetime without a time part. I don't think it's a good idea to select a string. The result set of the query you suggested, for example, would be interpreted as January 9 and September 9 in some cultures, and as September 1 and September 9 in others. My results would be presented and interpreted correctly regardless of culture.

SK

Perhaps English is your second or third language:

"How should I find the dates for 3 days past and 5 days future. Such as TodayDate-3 and TodayDate+5 base in the date only, discarding the time?"

|||

Adamus Turner@.discussions.microsoft.com wrote:
> This post has been edited either by the author or a moderator in the
> Microsoft Forums: http://forums.microsoft.com
>
> [quote user="Steve Kass"]Yes, I tested it.
>
> The query I posted was
>
> SELECT
> dateadd(day,datediff(day,0,getdate())-3,0) AS [3 Days Ago],
> dateadd(day,datediff(day,0,getdate())+5,0) AS [5 Days from now]
>
> The result in SQL Server (Query Analyzer, standard installation) is
>
> 3 Days Ago 5 Days from now
> - -
> 2006-09-01 00:00:00.000 2006-09-09 00:00:00.000
>
> Perhaps you think that the OP did not want the 00:00:00.000 to appear.
> Perhaps. Jim wanted to "find the dates," "discarding the time." There is
> no DATE data type in SQL Server, so I thought the best solution was to
> find the dates with midnight as the time. That's commonly referred to as
> a datetime without a time part. I don't think it's a good idea to select
> a string. The result set of the query you suggested, for example, would
> be interpreted as January 9 and September 9 in some cultures, and as
> September 1 and September 9 in others. My results would be presented and
> interpreted correctly regardless of culture.
>
> SK
> [/quote]The comment on culture is incorrect. Converting the date to a
> string will not change the compiler interpretation of midnight. In fact,
> it will set the time as midnight. At no time, and under no circumstance
> will the date be incorrect. I never said the time portion might be interpreted incorrectly. But you are simply wrong about the date. The string results of your code will represent wrong dates depending on the culture. Here is a repro using the code you posted to show that: set language French go create table AdamusDates ( [3 Days Ago] datetime, [5 days from now] datetime ) insert into AdamusDates SELECT convert(varchar(15), DATEADD( day, -3, GETDATE()), 101) AS [3 Days Ago], convert(varchar(15), DATEADD( day, 5, GETDATE()), 101) AS [5 days from now] set language English insert into AdamusDates SELECT convert(varchar(15), DATEADD( day, -3, GETDATE()), 101) AS [3 Days Ago], convert(varchar(15), DATEADD( day, 5, GETDATE()), 101) AS [5 days from now] go select * from AdamusDates go drop table AdamusDates Results: 3 Days Ago 5 days from now -- -- 2006-02-09 00:00:00.000 2006-10-09 00:00:00.000 2006-09-02 00:00:00.000 2006-09-10 00:00:00.000
>
> As a side note: It is very poor programming practice to misuse primitive
> functions such as dateadd() as some form of clever workaround to help
> justify an inability to correctly resolve an issue. DateDiff()? Dateadd() and Datediff() are well-documented and entirely appropriate to a question about dates. Apparently you would prefer to maintain your inability to correctly resolve the issue, as some form of obstinate resistance to the use of language elements that would solve the problem.
>
> In good programming ethics and to avoid maintenace confusion, this
> elementary trickery should be avoided at all costs. It's not trickery. It's a simple application of documented behavior of T-SQL. And it works in the US, in France, and elsewhere. SK
>
> Adamus
>
>

past and future date

How should I find the dates for 3 days past and 5 days future. Such as TodayDate-3 and TodayDate+5 base in the date only, discarding the time?

Getdate()-3 , Getdate()+5 ?|||

Discarding the time would be like:

DATEADD(d,0,DATEDIFF(d,0,getdate())-3) and DATEADD(d,0,DATEDIFF(d,0,getdate())+5)

|||

CONVERT(Varchar,getdate()-3, 101) shouldwork.

should work.

|||Using DATEADD/DATEDIFF is more generic because you can use the same thing to select the beginning of a day, week, month, or year. It's also keeps the result in a datetime object, is culture insensitive, and performs many times faster then the varchar methods.|||Thanks for the reply

Wednesday, March 7, 2012

Password Changes in SQLServer

Is there a way to find out if a user has not changed his/her passord in more than 90 days. I need to find the date of last password change and days since last password changes.
Thanks in Advance.The only way that I know to do this is to create a table with three columns, one for the login name, one for the current (hashed) password, and one for the date the last change was detected. Periodically (probably daily) check to see if there are either new logins or new password values.

-PatP|||Well, I wouldn't say that that's the only way. In fact, all you need is 2 columns, login and lastchanged. I've gone a little further and modified 3 system procedures, - sp_password, sp_addlogin, and sp_droplogin by implementing password aging and compexity. By having the table above you can incorporate UPDATE, INSERT, and DELETE into/from it respectively.|||You've got a point, but modifying system supplied stored procedures or triggers will get you a reserved seat in the "naughty" section in my opinion. I wouldn't consider recommending that, and probably wouldn't permit it in any environment that I can control.

-PatP|||Just use trusted connections|||You've got a point, but modifying system supplied stored procedures or triggers will get you a reserved seat in the "naughty" section in my opinion. I wouldn't consider recommending that, and probably wouldn't permit it in any environment that I can control.

-PatPThere are no system triggers. Modifying system stored procedures? If you know what you're doing, and if your Change Management process is in place, - I see no problem with that. After all, it's those modifications that prompted Yukon development team to implement...well, you'll see it in the next CTP.|||Come on...you can say it....

Better yet, if you're interested download 2005 BOL|||Too much inaccurate info there. It hasn't matched a single Beta-2 built yet ;)

Password Change Date

Is there a way to retreive the date and time when a user last time changed
his/her SQL password? I am using SQL 2000/SQL Server Authentication.
TIA
SorinHi,
SQL Server will not store those information.
Thanks
Hari
SQL Server MVP
"SOPONL" <SOPONL@.discussions.microsoft.com> wrote in message
news:65DF9A86-8711-404D-B6C9-930113378751@.microsoft.com...
> Is there a way to retreive the date and time when a user last time changed
> his/her SQL password? I am using SQL 2000/SQL Server Authentication.
> TIA
> Sorin|||Check out the updatedate column in syslogins. Other changes might change this as well, like changing
default language or default database, but I doubt that you do that very often.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"SOPONL" <SOPONL@.discussions.microsoft.com> wrote in message
news:65DF9A86-8711-404D-B6C9-930113378751@.microsoft.com...
> Is there a way to retreive the date and time when a user last time changed
> his/her SQL password? I am using SQL 2000/SQL Server Authentication.
> TIA
> Sorin

Password Change Date

Is there a way to retreive the date and time when a user last time changed
his/her SQL password? I am using SQL 2000/SQL Server Authentication.
TIA
SorinHi,
SQL Server will not store those information.
Thanks
Hari
SQL Server MVP
"SOPONL" <SOPONL@.discussions.microsoft.com> wrote in message
news:65DF9A86-8711-404D-B6C9-930113378751@.microsoft.com...
> Is there a way to retreive the date and time when a user last time changed
> his/her SQL password? I am using SQL 2000/SQL Server Authentication.
> TIA
> Sorin|||Check out the updatedate column in syslogins. Other changes might change thi
s as well, like changing
default language or default database, but I doubt that you do that very ofte
n.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"SOPONL" <SOPONL@.discussions.microsoft.com> wrote in message
news:65DF9A86-8711-404D-B6C9-930113378751@.microsoft.com...
> Is there a way to retreive the date and time when a user last time changed
> his/her SQL password? I am using SQL 2000/SQL Server Authentication.
> TIA
> Sorin

Password Change Date

Is there a way to retreive the date and time when a user last time changed
his/her SQL password? I am using SQL 2000/SQL Server Authentication.
TIA
Sorin
Hi,
SQL Server will not store those information.
Thanks
Hari
SQL Server MVP
"SOPONL" <SOPONL@.discussions.microsoft.com> wrote in message
news:65DF9A86-8711-404D-B6C9-930113378751@.microsoft.com...
> Is there a way to retreive the date and time when a user last time changed
> his/her SQL password? I am using SQL 2000/SQL Server Authentication.
> TIA
> Sorin
|||Check out the updatedate column in syslogins. Other changes might change this as well, like changing
default language or default database, but I doubt that you do that very often.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"SOPONL" <SOPONL@.discussions.microsoft.com> wrote in message
news:65DF9A86-8711-404D-B6C9-930113378751@.microsoft.com...
> Is there a way to retreive the date and time when a user last time changed
> his/her SQL password? I am using SQL 2000/SQL Server Authentication.
> TIA
> Sorin

Saturday, February 25, 2012

Passing table-type variables to SP?

Sorry about posting twice, but I had some trouble with setting the date on
this machine...
I would like to write a stored procedure that takes a table (i.e. table
variable) as its parameter.
E.g.:
DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
SP_MYPROC @.mytable
What should SP_MYPROC's signature look like? I can't seem to be able to find
anything about this in the documenation.
I guess it would look something like this:
ALTER PROCEDURE SP_MYPROC
(@.t TABLE)
or
ALTER PROCEDURE SP_MYPROC
(@.t TABLE(ID INT, NAME VARCHAR(50)))
Actually, how big is the freedom when taking such a parameter? Is it enough
to use the first version and treat the table whatever way I would like to?
E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
any tables the second column of which is of type VARCHAR of any size.
Hi
You can not pass a table data type to a stored procedure. You may want to
use temporary tables instead.
From Books online "Create procedure" topic
CREATE PROC [ EDURE ] [ owner. ] procedure_name [ ; number ]
[ { @.parameter data_type }
[ VARYING ] [ = default ] [ OUTPUT ]
] [ ,...n ]
[ WITH
{ RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]
[ FOR REPLICATION ]
AS sql_statement [ ...n ]
data_type
Is the parameter data type. All data types, except the table data type, can
be used as a parameter for a stored procedure. However, the cursor data type
can be used only on OUTPUT parameters. When you specify a data type of
cursor, the VARYING and OUTPUT keywords must also be specified. For more
information about SQL Server - supplied data types and their syntax, see
Data Types.
John
"Agoston Bejo" <gusz1@.freemail.hu> wrote in message
news:OppBdMHtEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Sorry about posting twice, but I had some trouble with setting the date on
> this machine...
> I would like to write a stored procedure that takes a table (i.e. table
> variable) as its parameter.
> E.g.:
> DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
> SP_MYPROC @.mytable
> What should SP_MYPROC's signature look like? I can't seem to be able to
find
> anything about this in the documenation.
> I guess it would look something like this:
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE)
> or
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE(ID INT, NAME VARCHAR(50)))
> Actually, how big is the freedom when taking such a parameter? Is it
enough
> to use the first version and treat the table whatever way I would like to?
> E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
> any tables the second column of which is of type VARCHAR of any size.
>
>
|||You cannot pass a table variable from one sp to another.
This article discuss the options to share data between procs.
http://www.sommarskog.se/share_data.html
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"Agoston Bejo" <gusz1@.freemail.hu> wrote in message
news:OppBdMHtEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Sorry about posting twice, but I had some trouble with setting the date on
> this machine...
> I would like to write a stored procedure that takes a table (i.e. table
> variable) as its parameter.
> E.g.:
> DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
> SP_MYPROC @.mytable
> What should SP_MYPROC's signature look like? I can't seem to be able to
> find
> anything about this in the documenation.
> I guess it would look something like this:
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE)
> or
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE(ID INT, NAME VARCHAR(50)))
> Actually, how big is the freedom when taking such a parameter? Is it
> enough
> to use the first version and treat the table whatever way I would like to?
> E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
> any tables the second column of which is of type VARCHAR of any size.
>
>

Passing table-type variables to SP?

Sorry about posting twice, but I had some trouble with setting the date on
this machine... :)
I would like to write a stored procedure that takes a table (i.e. table
variable) as its parameter.
E.g.:
DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
SP_MYPROC @.mytable
What should SP_MYPROC's signature look like? I can't seem to be able to find
anything about this in the documenation.
I guess it would look something like this:
ALTER PROCEDURE SP_MYPROC
(@.t TABLE)
or
ALTER PROCEDURE SP_MYPROC
(@.t TABLE(ID INT, NAME VARCHAR(50)))
Actually, how big is the freedom when taking such a parameter? Is it enough
to use the first version and treat the table whatever way I would like to?
E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
any tables the second column of which is of type VARCHAR of any size.Hi
You can not pass a table data type to a stored procedure. You may want to
use temporary tables instead.
From Books online "Create procedure" topic
CREATE PROC [ EDURE ] [ owner. ] procedure_name [ ; number ]
[ { @.parameter data_type }
[ VARYING ] [ = default ] [ OUTPUT ]
] [ ,...n ]
[ WITH
{ RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]
[ FOR REPLICATION ]
AS sql_statement [ ...n ]
data_type
Is the parameter data type. All data types, except the table data type, can
be used as a parameter for a stored procedure. However, the cursor data type
can be used only on OUTPUT parameters. When you specify a data type of
cursor, the VARYING and OUTPUT keywords must also be specified. For more
information about SQL Server - supplied data types and their syntax, see
Data Types.
John
"Agoston Bejo" <gusz1@.freemail.hu> wrote in message
news:OppBdMHtEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Sorry about posting twice, but I had some trouble with setting the date on
> this machine... :)
> I would like to write a stored procedure that takes a table (i.e. table
> variable) as its parameter.
> E.g.:
> DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
> SP_MYPROC @.mytable
> What should SP_MYPROC's signature look like? I can't seem to be able to
find
> anything about this in the documenation.
> I guess it would look something like this:
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE)
> or
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE(ID INT, NAME VARCHAR(50)))
> Actually, how big is the freedom when taking such a parameter? Is it
enough
> to use the first version and treat the table whatever way I would like to?
> E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
> any tables the second column of which is of type VARCHAR of any size.
>
>|||You cannot pass a table variable from one sp to another.
This article discuss the options to share data between procs.
http://www.sommarskog.se/share_data.html
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"Agoston Bejo" <gusz1@.freemail.hu> wrote in message
news:OppBdMHtEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Sorry about posting twice, but I had some trouble with setting the date on
> this machine... :)
> I would like to write a stored procedure that takes a table (i.e. table
> variable) as its parameter.
> E.g.:
> DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
> SP_MYPROC @.mytable
> What should SP_MYPROC's signature look like? I can't seem to be able to
> find
> anything about this in the documenation.
> I guess it would look something like this:
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE)
> or
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE(ID INT, NAME VARCHAR(50)))
> Actually, how big is the freedom when taking such a parameter? Is it
> enough
> to use the first version and treat the table whatever way I would like to?
> E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
> any tables the second column of which is of type VARCHAR of any size.
>
>

Passing table-type variables to SP?

Sorry about posting twice, but I had some trouble with setting the date on
this machine...
I would like to write a stored procedure that takes a table (i.e. table
variable) as its parameter.
E.g.:
DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
SP_MYPROC @.mytable
What should SP_MYPROC's signature look like? I can't seem to be able to find
anything about this in the documenation.
I guess it would look something like this:
ALTER PROCEDURE SP_MYPROC
(@.t TABLE)
or
ALTER PROCEDURE SP_MYPROC
(@.t TABLE(ID INT, NAME VARCHAR(50)))
Actually, how big is the freedom when taking such a parameter? Is it enough
to use the first version and treat the table whatever way I would like to?
E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
any tables the second column of which is of type VARCHAR of any size.
Hi
You can not pass a table data type to a stored procedure. You may want to
use temporary tables instead.
From Books online "Create procedure" topic
CREATE PROC [ EDURE ] [ owner. ] procedure_name [ ; number ]
[ { @.parameter data_type }
[ VARYING ] [ = default ] [ OUTPUT ]
] [ ,...n ]
[ WITH
{ RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]
[ FOR REPLICATION ]
AS sql_statement [ ...n ]
data_type
Is the parameter data type. All data types, except the table data type, can
be used as a parameter for a stored procedure. However, the cursor data type
can be used only on OUTPUT parameters. When you specify a data type of
cursor, the VARYING and OUTPUT keywords must also be specified. For more
information about SQL Server - supplied data types and their syntax, see
Data Types.
John
"Agoston Bejo" <gusz1@.freemail.hu> wrote in message
news:OppBdMHtEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Sorry about posting twice, but I had some trouble with setting the date on
> this machine...
> I would like to write a stored procedure that takes a table (i.e. table
> variable) as its parameter.
> E.g.:
> DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
> SP_MYPROC @.mytable
> What should SP_MYPROC's signature look like? I can't seem to be able to
find
> anything about this in the documenation.
> I guess it would look something like this:
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE)
> or
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE(ID INT, NAME VARCHAR(50)))
> Actually, how big is the freedom when taking such a parameter? Is it
enough
> to use the first version and treat the table whatever way I would like to?
> E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
> any tables the second column of which is of type VARCHAR of any size.
>
>
|||You cannot pass a table variable from one sp to another.
This article discuss the options to share data between procs.
http://www.sommarskog.se/share_data.html
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"Agoston Bejo" <gusz1@.freemail.hu> wrote in message
news:OppBdMHtEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Sorry about posting twice, but I had some trouble with setting the date on
> this machine...
> I would like to write a stored procedure that takes a table (i.e. table
> variable) as its parameter.
> E.g.:
> DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
> SP_MYPROC @.mytable
> What should SP_MYPROC's signature look like? I can't seem to be able to
> find
> anything about this in the documenation.
> I guess it would look something like this:
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE)
> or
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE(ID INT, NAME VARCHAR(50)))
> Actually, how big is the freedom when taking such a parameter? Is it
> enough
> to use the first version and treat the table whatever way I would like to?
> E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
> any tables the second column of which is of type VARCHAR of any size.
>
>

Passing table-type variables to SP?

Sorry about posting twice, but I had some trouble with setting the date on
this machine...
I would like to write a stored procedure that takes a table (i.e. table
variable) as its parameter.
E.g.:
DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
SP_MYPROC @.mytable
What should SP_MYPROC's signature look like? I can't seem to be able to find
anything about this in the documenation.
I guess it would look something like this:
ALTER PROCEDURE SP_MYPROC
(@.t TABLE)
or
ALTER PROCEDURE SP_MYPROC
(@.t TABLE(ID INT, NAME VARCHAR(50)))
Actually, how big is the freedom when taking such a parameter? Is it enough
to use the first version and treat the table whatever way I would like to?
E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
any tables the second column of which is of type VARCHAR of any size.Hi
You can not pass a table data type to a stored procedure. You may want to
use temporary tables instead.
From Books online "Create procedure" topic
CREATE PROC [ EDURE ] [ owner. ] procedure_name [ ; number ]
[ { @.parameter data_type }
[ VARYING ] [ = default ] [ OUTPUT ]
] [ ,...n ]
[ WITH
{ RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]
[ FOR REPLICATION ]
AS sql_statement [ ...n ]
data_type
Is the parameter data type. All data types, except the table data type, can
be used as a parameter for a stored procedure. However, the cursor data type
can be used only on OUTPUT parameters. When you specify a data type of
cursor, the VARYING and OUTPUT keywords must also be specified. For more
information about SQL Server - supplied data types and their syntax, see
Data Types.
John
"Agoston Bejo" <gusz1@.freemail.hu> wrote in message
news:OppBdMHtEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Sorry about posting twice, but I had some trouble with setting the date on
> this machine...
> I would like to write a stored procedure that takes a table (i.e. table
> variable) as its parameter.
> E.g.:
> DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
> SP_MYPROC @.mytable
> What should SP_MYPROC's signature look like? I can't seem to be able to
find
> anything about this in the documenation.
> I guess it would look something like this:
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE)
> or
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE(ID INT, NAME VARCHAR(50)))
> Actually, how big is the freedom when taking such a parameter? Is it
enough
> to use the first version and treat the table whatever way I would like to?
> E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
> any tables the second column of which is of type VARCHAR of any size.
>
>|||You cannot pass a table variable from one sp to another.
This article discuss the options to share data between procs.
http://www.sommarskog.se/share_data.html
Roji. P. Thomas
Net Asset Management
https://www.netassetmanagement.com
"Agoston Bejo" <gusz1@.freemail.hu> wrote in message
news:OppBdMHtEHA.4040@.TK2MSFTNGP09.phx.gbl...
> Sorry about posting twice, but I had some trouble with setting the date on
> this machine...
> I would like to write a stored procedure that takes a table (i.e. table
> variable) as its parameter.
> E.g.:
> DECLARE @.mytable = TABLE(ID INT, NAME VARCHAR(50))
> SP_MYPROC @.mytable
> What should SP_MYPROC's signature look like? I can't seem to be able to
> find
> anything about this in the documenation.
> I guess it would look something like this:
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE)
> or
> ALTER PROCEDURE SP_MYPROC
> (@.t TABLE(ID INT, NAME VARCHAR(50)))
> Actually, how big is the freedom when taking such a parameter? Is it
> enough
> to use the first version and treat the table whatever way I would like to?
> E.g. not assuming that the 'NAME' column is a VARCHAR(50), but taking
> any tables the second column of which is of type VARCHAR of any size.
>
>

Monday, February 20, 2012

passing table name as parameter for SP - asap


Hi all,
i have SP where i need to pass the tablename and the date as the
parameter
i started like this
CREATE PROC SP_DEL_STG_TAB
@.TABLE_NAME varchar(50) ,
@.PRESENT_REC_LD_DT DATETIME
AS
EXEC ( 'DELETE FROM '+ @.TABLE_NAME +' WHERE LOAD_DT =' +
@.PRESENT_REC_LD_DT)
GO
when i complied i did not find error but during the execution by passing
the parameters using
EXEC SP_DEL_STG_TAB 'STG_UNSUCCESS_LOGINS', '7/3/2004'
it gives an wiered error
Line 1: Incorrect syntax near '3'.
please help me on this as early as possible i am stuck at this point.
thanks in advance
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!Hi
Use YYYYMMDD format for dates.
"sushma B V" <bv.sushma@.iflexsolutions.com> wrote in message
news:%23msW6xBFFHA.624@.TK2MSFTNGP15.phx.gbl...
>
> Hi all,
> i have SP where i need to pass the tablename and the date as the
> parameter
> i started like this
> CREATE PROC SP_DEL_STG_TAB
> @.TABLE_NAME varchar(50) ,
> @.PRESENT_REC_LD_DT DATETIME
> AS
> EXEC ( 'DELETE FROM '+ @.TABLE_NAME +' WHERE LOAD_DT =' +
> @.PRESENT_REC_LD_DT)
> GO
> when i complied i did not find error but during the execution by passing
> the parameters using
> EXEC SP_DEL_STG_TAB 'STG_UNSUCCESS_LOGINS', '7/3/2004'
> it gives an wiered error
> Line 1: Incorrect syntax near '3'.
> please help me on this as early as possible i am stuck at this point.
> thanks in advance
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!|||Try to use a string variable and build the query, then execute it using exec
command
Babu M K
"sushma B V" <bv.sushma@.iflexsolutions.com> wrote in message
news:%23msW6xBFFHA.624@.TK2MSFTNGP15.phx.gbl...
>
> Hi all,
> i have SP where i need to pass the tablename and the date as the
> parameter
> i started like this
> CREATE PROC SP_DEL_STG_TAB
> @.TABLE_NAME varchar(50) ,
> @.PRESENT_REC_LD_DT DATETIME
> AS
> EXEC ( 'DELETE FROM '+ @.TABLE_NAME +' WHERE LOAD_DT =' +
> @.PRESENT_REC_LD_DT)
> GO
> when i complied i did not find error but during the execution by passing
> the parameters using
> EXEC SP_DEL_STG_TAB 'STG_UNSUCCESS_LOGINS', '7/3/2004'
> it gives an wiered error
> Line 1: Incorrect syntax near '3'.
> please help me on this as early as possible i am stuck at this point.
> thanks in advance
> *** Sent via Developersdex http://www.examnotes.net ***
> Don't just participate in USENET...get rewarded for it!