Showing posts with label storedprocedure. Show all posts
Showing posts with label storedprocedure. Show all posts

Wednesday, March 28, 2012

No formulas in Stored Procedure params?

Hi,
is there any reason why it is not allowed to use formulas as Stored
Procedure parameters?
e.g.: EXEC myProc(@.myVar + ' ' + @.myVar2)
I could make use of this feature over and over if SQL Server 2k would allow
me to.
Axel DahmenI believe this has to do with the way the compiler works. By the time
you've hit the EXEC, it's too late to evaluate the expression. But it's
really not too bad:
DECLARE @.myVar3 VARCHAR(300)
SET @.MyVar3 = @.myVar + ' ' + @.myVar2
EXEC myProc @.MyVar3
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
"Axel Dahmen" <NO_SPAM@.NoOneKnows.invalid> wrote in message
news:uDecFY3PFHA.648@.TK2MSFTNGP14.phx.gbl...
> Hi,
> is there any reason why it is not allowed to use formulas as Stored
> Procedure parameters?
> e.g.: EXEC myProc(@.myVar + ' ' + @.myVar2)
> I could make use of this feature over and over if SQL Server 2k would
allow
> me to.
> Axel Dahmen
>|||Thanks, Adam.
I must admit that I'm not too satisfied with the current way of handling
this. I feel it's fairly awkward to write a bunch of lines like:
DECLARE @.myVar3 VARCHAR(300)
SET @.MyVar3 = @.myVar + ' ' + @.myVar2
EXEC myProc @.MyVar3
instead of just simply:
EXEC myProc @.myVar + ' ' + @.myVar2
particularly if you do things on-the-fly.
Do you perhaps know if SQL Server 2k5 will be able to process expressions in
procedure/UDF calls? If it doesn't, do you probably know about some channel
to suggest this functionality? It's nagging me for quite a while now. And
perhaps others might find a change in parsing procedure/UDF calls useful,
too.
Best wishes,
Axel
"Adam Machanic" <amachanic@.hotmail._removetoemail_.com> schrieb im
Newsbeitrag news:#5ZoPd3PFHA.3196@.TK2MSFTNGP12.phx.gbl...
> I believe this has to do with the way the compiler works. By the time
> you've hit the EXEC, it's too late to evaluate the expression. But it's
> really not too bad:
> DECLARE @.myVar3 VARCHAR(300)
> SET @.MyVar3 = @.myVar + ' ' + @.myVar2
> EXEC myProc @.MyVar3
>
> --
> Adam Machanic
> SQL Server MVP
> http://www.datamanipulation.net
> --
>
> "Axel Dahmen" <NO_SPAM@.NoOneKnows.invalid> wrote in message
> news:uDecFY3PFHA.648@.TK2MSFTNGP14.phx.gbl...
> allow
>|||> I feel it's fairly awkward
But necessary, so what are you going to do?
A workaround would be dynamic SQL, but it becomes quite complex as you have
varying data types being passed to the stored procedure.

> Do you perhaps know if SQL Server 2k5 will be able to process expressions
> in
> procedure/UDF calls?
No.

> If it doesn't, do you probably know about some channel
> to suggest this functionality?
sqlwish@.microsoft.com

> It's nagging me for quite a while now. And
> perhaps others might find a change in parsing procedure/UDF calls useful,
> too.
Frankly, the known workarounds are really not all that cumbersome, and I
think most people have just learned to live with the limitation and have
moved on. This sounds like the same kind of problem we have when people use
SELECT * or otherwise try avoid "all the hard work" of typing out a column
list.
This is my signature. It is a general reminder.
Please post DDL, sample data and desired results.
See http://www.aspfaq.com/5006 for info.|||"Axel Dahmen" <NO_SPAM@.NoOneKnows.invalid> wrote in message
news:uOU1kA4PFHA.2604@.TK2MSFTNGP10.phx.gbl...
> Do you perhaps know if SQL Server 2k5 will be able to process expressions
in
> procedure/UDF calls? If it doesn't, do you probably know about some
channel
> to suggest this functionality? It's nagging me for quite a while now. And
> perhaps others might find a change in parsing procedure/UDF calls useful,
> too.
No, the behavior won't change in SS2005 (at least, not based on anything
I've heard, and I've also just tested on the latest build -- same behavior).
I believe -- although someone else can hopefully back me up on this as I
don't know for certain -- that the behavior complies to the ANSI standard.
And if the standard doesn't support that, I wouldn't expect SQL Server to.
As I said, it has to do with the way the compiler works. SQL is compiled
using a very simple single-pass method. Re-doing that compilation for the
sake of this feature is not really a good use of time. There are a few
other features that could be done with multi-pass compilation, but I don't
think the SQL Server team would agree with me on how interesting I think
some of them are ;)
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--

> Best wishes,
> Axel
>
> --
> "Adam Machanic" <amachanic@.hotmail._removetoemail_.com> schrieb im
> Newsbeitrag news:#5ZoPd3PFHA.3196@.TK2MSFTNGP12.phx.gbl...
>|||I might be ignorant here, but even after giving it some deep thoughts I
don't find a reason why it should give the interpreter a hard time to accept
expressions instead of identifiers as procedure parameters.
The internal SQL Server part executing the SET statement could be used, for
instance, to interpret a given expression. The interpreter might create some
temporary dummy variable first to store the calculation result internally.
When calling a procedure/UDF a data type transformation call is triggered
internally for each parameter. So it's safe to assume that a parameter's
data format is already known to the interpreter. It should be easy then to
create such internal dummy variable to store the expression result and to
forward that result to the standard procedure/UDF call.
It would even be possible to have no additional execution costs compared to
the present (lacking)implementation. A cache could be implemented to hold
all temporary variables for the next call to the same procedure within the
same batch.
Program Flow:
- prepare procedure call
- WHILE parameter read
- if parameter is no legal identifier then
- create dummy variable to hold parameter
- SET {dummy variable} = parameter
- overwrite parameter to point to dummy variable
- add parameter to procedure call
- call procedure
RFC,
Axel Dahmen
www.sportbootcharter.com
"Adam Machanic" <amachanic@.hotmail._removetoemail_.com> schrieb im
Newsbeitrag news:#OH8lR4PFHA.2560@.TK2MSFTNGP14.phx.gbl...
> "Axel Dahmen" <NO_SPAM@.NoOneKnows.invalid> wrote in message
> news:uOU1kA4PFHA.2604@.TK2MSFTNGP10.phx.gbl...
expressions
> in
> channel
And
useful,
> No, the behavior won't change in SS2005 (at least, not based on
anything
> I've heard, and I've also just tested on the latest build -- same
behavior).
> I believe -- although someone else can hopefully back me up on this as I
> don't know for certain -- that the behavior complies to the ANSI standard.
> And if the standard doesn't support that, I wouldn't expect SQL Server to.
> As I said, it has to do with the way the compiler works. SQL is compiled
> using a very simple single-pass method. Re-doing that compilation for the
> sake of this feature is not really a good use of time. There are a few
> other features that could be done with multi-pass compilation, but I don't
> think the SQL Server team would agree with me on how interesting I think
> some of them are ;)
>
> --
> Adam Machanic
> SQL Server MVP
> http://www.datamanipulation.net
> --
>
it's
would
>|||>> I might be ignorant here, but even after giving it some deep
thoughts I don't find a reason why it should give the interpreter a
hard time to accept expressions instead of identifiers as procedure
parameters. <<
In the early days of Sybase/SQL Server, we had a rule that you never
wrote over one page of T-SQL in a stored procedure or a trigger. It
was a kludge to get around the lack of DRI actions, DEFAULT, CHECK(),
etc, back in the Stone Age. It was never meant to be a full language.
The truth is that T-SQL was an early, one-pass compiler based on C; it
was not meant to do Thunks. One -pass compilers have a hell of a time
with Thunks; it takes two passes in most cases. Today T-SQL would be a
one-quarter senior project using a Compiler compiler, YACC or whatever.
Full strength application development languages came later. The
Informix 4GL isbased on the Algol family and a lot of mainframe
packages use it under the covers; it generated ANSI Standard C and is
very portable, has a report writer, etc. Progress started as a nice
application development 4GL which added SQL. PL/SQL in Oracle and the
Standard SQL/PSM are both based on ADA.
What we need is the Standard SQL/PSM instead of the CLR crap that is
about to destroy SQL Server databases (I have some anti-CLR rants
posted in various places).|||> The truth is that T-SQL was an early, one-pass compiler based on C; it
> was not meant to do Thunks. One -pass compilers have a hell of a time
> with Thunks; it takes two passes in most cases.
I appreciate that. But the SET command is already able do it. Thus I assume
the only change in procedure call processing should basically be to call the
SET command part after reading a parameter token. Perhaps there even is a
general expression parser part available in the interpreter that can be used
by T-SQL language coders at MS.
My main assumption is that:
if the T-SQL interpreter can process this (manual) sequence in one pass
DECLARE @.var ...
SET @.var = {expression}
EXEC proc @.var
then it can also do this automatically itself.
The most simple (and farmost worst) option would be to just expand any
parameter call to the above sequence internally. This deduction is my proof
of concept.

> What we need is the Standard SQL/PSM instead of the CLR crap that is
> about to destroy SQL Server databases (I have some anti-CLR rants
> posted in various places).
That's another story, but you're right, I agree with you. Giving too much
programming power to SQL Server will probably lead into decadence in design
in terms of a multi-tier model. And I'm also apprehensive of SQL Server 2005
becoming a slow performer and thus less scalable. Any Framework,
particularly .NET and Java by its nature of using IL, is slow and clumsy.
Well, it's a matter of consideration between RAD and performance. But enough
OT... ;)
Axel|||>> if the T-SQL interpreter can process this (manual) sequence in one
pass ... then it can also do this automatically itself. <<
Did you take a compiler writing course in college? I taught one :)
The simple ase is easy; the general case is a bear!
The compiler has to catch the general expression in the parameter,
adjust the scope of the declarations, fill in parameters in any
function calls, recursive function calls, self-references, etc. It
is not a simple matter of replacing a marker with "((<< expression
T-SQL sets error flags one statement at a time; SQL/PSM and modern
languages set multiple errors all at once based on BEGIN ATOMIC .. END
blocks. T-SQL was never meant for "industrial usage" in a modern
programming environment.|||> One -pass compilers have a hell of a time
> with Thunks; it takes two passes in most cases.
It took a few moments for me to see what you mean... The trick would be to
assume a destination data type to store the expression result in.
I guess there will be a feasible solution to that. For instance, every
expression calculated by SQL Server has an intrinsic data type. During
interpretation of the SET command the result is probably forwarded to the
data type converter part after calculation of the expression result. To
create a matching temporary memory space (or variable), this step ought to
be injected in between:
Assumed Standard SET Command Program Flow:
1) read destination variable
2) lookup destination variable in memory
3) read expression to be assigned
4) calculate expression
5) forward expression result and destination variable to
data type converter in order to store the converted
result into destination variable
To allow for dynamic destination variable creation, the flow could be
interjected with something like (one additional step added):
1) read expression to be assigned
2) calculate expression
* 3) create temporary destination variable from expression
data type
4) forward expression result and destination variable to
data type converter in order to store the converted
result into destination variable
5) provide destination variable to stored procedure
parameter
Regards,
Axel

Monday, March 26, 2012

no date retrun

hi all i have question about this procedure

when i write this the data return this is nice ok

USE [DigitalFirm]

GO

/****** Object: StoredProcedure [dbo].[SearchTaskName] Script Date: 05/31/2007 21:44:47 ******/

SETANSI_NULLSON

GO

SETQUOTED_IDENTIFIERON

GO

ALTERProcedure [dbo].[SearchTaskName]

(

@.TaskNamevarchar(50)

)

as

select a.TaskId,a.TaskName,p.fullName,a.Sdate,a.Fdate

from Assign_Task a,Task_Emp te,Personal p,Employee e

where te.TaskId=a.TaskIdand te.IDNO=e.IDNOand p.SSN=e.IDNOand a.TaskName=@.TaskName

and when i write this procedure in Sql server 2005

the i can show data and it is right and work

then when i want to exam this procedur in my web no have any data

please tell me why??

USE [DigitalFirm]

GO

/****** Object: StoredProcedure [dbo].[SearchTaskName] Script Date: 05/31/2007 21:44:47 ******/

SETANSI_NULLSON

GO

SETQUOTED_IDENTIFIERON

GO

ALTERProcedure [dbo].[SearchTaskName]

(

@.TaskNamevarchar(50)

)

as

select a.TaskId,a.TaskName,p.fullName,a.Sdate,a.Fdate

from Assign_Task a,Task_Emp te,Personal p,Employee e

where te.TaskId=a.TaskIdand te.IDNO=e.IDNOand p.SSN=e.IDNOand a.TaskNamelike'@.TaskName%'

thanx

Try this:

select a.TaskId,a.TaskName,p.fullName,a.Sdate,a.Fdatefrom Assign_Task a,Task_Emp te,Personal p,Employee ewhere te.TaskId=a.TaskIdand te.IDNO=e.IDNOand p.SSN=e.IDNOand a.TaskNamelike @.TaskName +'%'
|||

hi thanx mrndinakar you are nice manEmbarrassed

this is nice help for me .Stick out tongue

thanx

|||

it_amani:

hi thanx mrndinakar you are nice manEmbarrassed

this is nice help for me .Stick out tongue

thanx

You are welcome.Smile

|||

thax

mmmm

i want ask about another problem

when i update record from my web and i have value is null , the record not update

how i can solve this problem

thanx mrndinakar

|||

There could be lot of things involved. Post the UPDATE script you have. Does the script work if you run it from query analyzer?

|||

thanx i solve it . in incurved way(in arabic language toroq moltaweya)

but i wana to put my code and my query

Try

Dim idAsString

id = Request.QueryString("id").ToString()

If CheckBox1.Checked =TrueThen

fun.Update_Employee(id, fullname.Text, jobtitle.Text, salary.Text, sdate.Text, F_Date.Text, depno.SelectedValue, Active.SelectedValue, Username.Text, Pass.Text, Type.SelectedValue)

Label3.Text ="Update Employee Information are sucessful"

Else

fun.Update_Employee1(id, fullname.Text, jobtitle.Text, salary.Text, sdate.Text, depno.SelectedValue, Active.SelectedValue, Username.Text, Pass.Text, Type.SelectedValue)

Label3.Text ="Update Employee Information are sucessful"

EndIf

this is to the first Update

ALTERProcedure [dbo].[update_Employee]

(

@.IDNOnumeric(18,0),

@.fullNamevarchar(50),

@.Job_Titlevarchar(50),

@.Salaryint,

@.S_Datedatetime,

@.F_Datedatetime,

@.DepNonumeric(18,0),

@.Activebit,

@.Usernamevarchar(20),

@.Passvarchar(20),

@.ID_Type_idnumeric(18,0)

)

As

update Employeeset Job_Title=@.Job_Title,Salary=@.Salary,S_Date=@.S_Date,F_Date=@.F_Date,DepNo=@.DepNo,active=@.Active

where IDNO=@.IDNO

updateLogInset Username=@.Username,Pass=@.Pass,ID_Type_id=@.ID_Type_id

Where IDNO=@.IDNO

update Personalset fullName=@.fullName

Where SSN=@.IDNO

Catch exAs Exception

Label3.Text = ex.Message

and this to the second Update

ALTERProcedure [dbo].[update_Employee2]

(

@.IDNOnumeric(18,0),

@.fullNamevarchar(50),

@.Job_Titlevarchar(50),

@.Salaryint,

@.S_Datedatetime,

@.DepNonumeric(18,0),

@.Activebit,

@.Usernamevarchar(20),

@.Passvarchar(20),

@.ID_Type_idnumeric(18,0)

)

As

update Employeeset Job_Title=@.Job_Title,Salary=@.Salary,S_Date=@.S_Date,DepNo=@.DepNo,active=@.Active

where IDNO=@.IDNO

updateLogInset Username=@.Username,Pass=@.Pass,ID_Type_id=@.ID_Type_id

Where IDNO=@.IDNO

update Personalset fullName=@.fullName

WhereSSN=@.IDNO

thanx

Wednesday, March 7, 2012

newby - AUTOINCREMENT problem.

Hi,

I'm enclosed a snippet of test code which highlights my problem. The Stored
procedure insertValue should insert text into the parent, then insert other
text into the child table but the 2 tables should auto increment in sync
(i.e. so that they both end up with the same id numbers). I've tried taking
the auto increment out of the child table but then I don't know how to get
the right parent id into the child table.

Any advice appreciated - this is my first database, so I'm just in the
learning process really. Code follows:

CREATE TABLE Parent
(id INTEGER DEFAULT AUTOINCREMENT,
parenttext VARCHAR(16),
PRIMARY KEY (id))!

CREATE TABLE Child
(childID INTEGER INTEGER DEFAULT AUTOINCREMENT,
childtext VARCHAR(16),
FOREIGN KEY (childid) REFERENCES Parent(id),
PRIMARY KEY (childID))!

CREATE PROCEDURE insertValues(in p VARCHAR(16), in c VARCHAR(16))
BEGIN
insert into parent (parenttext) values (p);
insert into child (childtext) values (c);
END!

call insertValues('from parent', 'from child')!
select * from parent, child where parent.id = child.childid!In message <436cefc6$1_3@.mk-nntp-2.news.uk.tiscali.com>, Mary Walker
<123@.123.com> writes
>Hi,
>I'm enclosed a snippet of test code which highlights my problem. The Stored
>procedure insertValue should insert text into the parent, then insert other
>text into the child table but the 2 tables should auto increment in sync
>(i.e. so that they both end up with the same id numbers). I've tried taking
>the auto increment out of the child table but then I don't know how to get
>the right parent id into the child table.
>Any advice appreciated - this is my first database, so I'm just in the
>learning process really. Code follows:

OK. The first piece of advice is don't rely on the autonumber to return
any particular value. There are ways that the values in the two tables
could get out of sync and it would be a PITA to fix.

There are two options that I could suggest.

One is that you create the parent record and then take the autonumber
value from there and explicitly insert it into the child table. Don't
use an autonumber in child table, use a simple integer instead. This is
the way I recommend that you do it.

The second method has limited applicability but can make things easier.
First redesign your child table with an integer key. Then create a query
that links the two tables. You can now update the query instead of
updating the two tables separately.

--
Bernard Peek
London, UK. DBA, Manager, Trainer & Author.|||>> this is my first database, so I'm just in the learning process really. <<

Get a book on RDBMS and learn why an auto-increment can NEVER be a
relational key. Learn why a table name should be a collective or
plural name (unless there is only one row in the table). Learn way
"id" is too vague to be data element name. Learn why camelCase is a
bitch to read --hint: where does your eye jump when you see an
Uppercase letter?

I know you want to have a "Magical Universal Key" that will solve all
your design problems, without having to really think or learn anything.
What is your **real key** in your **real** problem?

Going to a Newsgroup to get what usually takes a few YEARS of college
and experience does not work.|||"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1131243682.809956.301200@.g49g2000cwa.googlegr oups.com...
>>> this is my first database, so I'm just in the learning process really.
>>> <<
> Get a book on RDBMS and learn why an auto-increment can NEVER be a
> relational key. Learn why a table name should be a collective or
> plural name (unless there is only one row in the table). Learn way
> "id" is too vague to be data element name. Learn why camelCase is a
> bitch to read --hint: where does your eye jump when you see an
> Uppercase letter?
> I know you want to have a "Magical Universal Key" that will solve all
> your design problems, without having to really think or learn anything.
> What is your **real key** in your **real** problem?
> Going to a Newsgroup to get what usually takes a few YEARS of college
> and experience does not work.

LOL. Thanks very much you for reply. I have actually learnt a lot by
simply reading your reply - but I'll buy the book anyway :-)|||In your book 'sql for smarties' you have an example with a table named
'Warehouse' - if you where to follow your own naming standards then it
should be called 'Warehouses'.

'id' is fine within the context of the table, if the column belongs to the
table 'Child' then its obviously Child.id.

What key would you use for a message board? Would you still key it on
subject and posting date or like the rest of us and how NNTP works, create a
guid with the domain?

The auto-number (IDENTITY property) is usually used as a artificial (or
surrogate) key, there is no such thing as a relational key - I think you
mean 'natural key', see: http://en.wikipedia.org/wiki/Natural_key.

Quoting from that aritcle...

"
The main disadvantage of choosing a natural key is that it may need to
change if your business requirements change. For example, if you have chosen
CustomerNumber as the primary key for a customer, and, subsequently,
CustomerNumber becomes alphanumeric instead of numeric, then as well as
changing the type of the column, you will need to make changes to all other
tables where CustomerNumber is used as a foreign key.

Retrieved from "http://en.wikipedia.org/wiki/Natural_key"

"

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials

"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1131243682.809956.301200@.g49g2000cwa.googlegr oups.com...
>>> this is my first database, so I'm just in the learning process really.
>>> <<
> Get a book on RDBMS and learn why an auto-increment can NEVER be a
> relational key. Learn why a table name should be a collective or
> plural name (unless there is only one row in the table). Learn way
> "id" is too vague to be data element name. Learn why camelCase is a
> bitch to read --hint: where does your eye jump when you see an
> Uppercase letter?
> I know you want to have a "Magical Universal Key" that will solve all
> your design problems, without having to really think or learn anything.
> What is your **real key** in your **real** problem?
> Going to a Newsgroup to get what usually takes a few YEARS of college
> and experience does not work.|||Hi Mary,

The SCOPE_IDENTITY() will return the last inserted IDENTITY value for that
connection.

insert into parent (parenttext) values (p);
print scope_identity() -- gives the id from parent

insert into child (childtext) values (c);
print scope_identity() -- gives id from child

Does that help?

Tony.

--
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials

"Mary Walker" <123@.123.com> wrote in message
news:436cefc6$1_3@.mk-nntp-2.news.uk.tiscali.com...
> Hi,
> I'm enclosed a snippet of test code which highlights my problem. The
> Stored procedure insertValue should insert text into the parent, then
> insert other text into the child table but the 2 tables should auto
> increment in sync (i.e. so that they both end up with the same id
> numbers). I've tried taking the auto increment out of the child table but
> then I don't know how to get the right parent id into the child table.
> Any advice appreciated - this is my first database, so I'm just in the
> learning process really. Code follows:
> CREATE TABLE Parent
> (id INTEGER DEFAULT AUTOINCREMENT,
> parenttext VARCHAR(16),
> PRIMARY KEY (id))!
> CREATE TABLE Child
> (childID INTEGER INTEGER DEFAULT AUTOINCREMENT,
> childtext VARCHAR(16),
> FOREIGN KEY (childid) REFERENCES Parent(id),
> PRIMARY KEY (childID))!
> CREATE PROCEDURE insertValues(in p VARCHAR(16), in c VARCHAR(16))
> BEGIN
> insert into parent (parenttext) values (p);
> insert into child (childtext) values (c);
> END!
> call insertValues('from parent', 'from child')!
> select * from parent, child where parent.id = child.childid!