Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Wednesday, March 7, 2012

DDL create user in T-SQL stored proc/trigger

Hello SQL Server programming gurus:

I am trying to create a trigger that fires after a user logon and logoff and does the following:

creates a new user
deletes data from old temp table

Then I need to create a stored procedure that executes dynamically to
drop old users
remove the old user account access

We are running SQL Server 2000.
Since I am new to T-SQL programming could anyone help point me in the right direction? Can I write dynamic SQL in a trigger to do these things?I really don't think you want to try that using a trigger. It sounds like a really bad idea to me.

First and foremost, I don't know of any way to launch a trigger on a login or logout event. You could probably approximate this using either SQL Profiler or a "watchdog" process, but the fundamental idea isn't supported by SQL Server itself, so you'll need some kind of "helper" to get the job done.

Next, I'd be really leary of having users created "on the fly" by anyone, much less what you seem to be describing where every login has the ability to create users! I can't imagine a scenario where that idea would appeal to me, unless it was to torment some dba that was going to inherit that code after I'd left. The thought positively gives me the willies!

The temp tables really aren't any problem... SQL Server simply drops temp tables after the user logs off. You really don't need to do any "care and feeding" of them, they are completely expendable.

If you can explain what you are trying to do (in English, not Transact-SQL), I'd be willing to bet that someone here can offer you some ideas that will help a bunch!

-PatP

Friday, February 24, 2012

DBPROPSET-> ???

I am doing some low level OLE DB programming to extract data out of MsSql 20
00 and need to find out what the DBPROPSET_DBINIT
value such as DBPROP_AUTH_USERID would be for the ADODB connectionstring equ
ivilant of 'Application name=' is. I can not find
matching property ID to be used in the DBPROPSET constants. It seems like it
should be DBPROP_APP_NAME but I can not find
anything described as such.
Does anyone have any idea what I need to use to have the 'ApplicationName' c
olumn in the Sql Profiler display the value for
a native OLE DB connection?
Dennis PassmoreI guess I am talking to myself again.
I found a answer that seems to work for now at least. Using the
DBPROP_INIT_PROVIDERSTRING identifier and setting the value to the
syntax of 'APP=myprogramname' is working.
Dennis Passmore

Tuesday, February 14, 2012

dbinit(), dblogin(), how often?

I'm a MS SQL newbie and am programming SQL using MS DS C++ 2003.

I'm writing sql code that will reside in a shared dll, used by many
processes and many threads in those processes.

So how often do I need to call dbinit()? Only the first time the DLL is
loaded, once per new process, once per thread, or once per database open?

Same question for dblogin().

Thanks very much for any help.
Bruce.Bruce. (noone@.nowhere.com) writes:

Quote:

Originally Posted by

I'm a MS SQL newbie and am programming SQL using MS DS C++ 2003.
>
I'm writing sql code that will reside in a shared dll, used by many
processes and many threads in those processes.
>
So how often do I need to call dbinit()? Only the first time the DLL is
loaded, once per new process, once per thread, or once per database open?
>
Same question for dblogin().


Zero times. At least unless you have some very special reason to use
DB-Library at all, like the need to support a legacy application. To wit,
DB-Library is a deprecated client API, and it lacks support for new features
added since SQL7, as Microsoft has not touched it for the last 8-10 years.

The recommended choice for a C++ application are ODBC and OLE DB. Of these
the ODBC is probably a lot easier to work with.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||"Erland Sommarskog" <esquel@.sommarskog.sewrote in message
news:Xns99425A6E7696FYazorman@.127.0.0.1...

Quote:

Originally Posted by

The recommended choice for a C++ application are ODBC and OLE DB. Of these
the ODBC is probably a lot easier to work with.


Not an option in this case but thanks for your reply anyway.

Bruce.|||Bruce. (noone@.nowhere.com) writes:

Quote:

Originally Posted by

"Erland Sommarskog" <esquel@.sommarskog.sewrote in message
news:Xns99425A6E7696FYazorman@.127.0.0.1...

Quote:

Originally Posted by

>The recommended choice for a C++ application are ODBC and OLE DB. Of
>these the ODBC is probably a lot easier to work with.


>
Not an option in this case but thanks for your reply anyway.


I'm sorry I was not able to answer your actual question at the time, but
I did not have access to some old source code that I have. Having looked
at that one, I see that I have this:

// Init DB-Library if we are the first player.
EnterCriticalSection(&CS);
if (no_of_threads++ == 0) {
if(dbinit() == FAIL) {
croak("Can't initialize dblibrary...");
}
// Set up the error handlers once for all.
dberrhandle(err_handler);
dbmsghandle(msg_handler);
}
LeaveCriticalSection(&CS);

// Set up LOGINREC struct for this thread.
td->login = dblogin();
DBSETLUSER(td->login, NULL);
DBSETLPWD(td->login, NULL);
DBSETLHOST(td->login, getenv("COMPUTERNAME"));

That is, call dbinit() when the DLL is initiated, but call dblogin once
for each thread. Then again, I guess the reason I did it this way was
to permit different threads to use the different login information. If
all threads will use the same login details, I can't see anything else
than that it would be sufficient to call dblogin() once, since LOGINREC
appears to only hold static data.

But permit me again to point the unsuitable in using DB-Library for new
development. Or to be more blunt: it's sheer silliness. If nothing else,
it's a waste of time for your professional development. The likelyhood
that you will get the oppurtunity to reuse the knowledge of DB-Library
programming are slim, whereas learning to master the ODBC API can be very
useful.

Why would ODBC or OLE DB not be an option in your case?

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||"Erland Sommarskog" <esquel@.sommarskog.sewrote in message
news:Xns99436DC7B5A82Yazorman@.127.0.0.1...

Quote:

Originally Posted by

That is, call dbinit() when the DLL is initiated, but call dblogin once
for each thread. Then again, I guess the reason I did it this way was
to permit different threads to use the different login information. If
all threads will use the same login details, I can't see anything else
than that it would be sufficient to call dblogin() once, since LOGINREC
appears to only hold static data.


That's very interesting and helpful. Thanks for the information.

Bruce.