Thursday, March 29, 2012
Get all User Databases
logged in user has rights to. I want a stored procedure that will return
just the names of all the non-system databases that I can populate a
dropdown list with. I really don't want to add the user to every database
or give them an Administrator role just to see the user table names. Any
help on this would be much appreciated.
JohnHi, John
Use something like this:
SELECT name FROM master.dbo.sysdatabases
WHERE HAS_DBACCESS(name) = 1
AND name NOT IN ('master','tempdb','model','msdb')
ORDER BY name
Razvan|||John,
Try:
SELECT NAME FROM MASTER..SYSDATABASES
WHERE NAME NOT IN ('MASTER','MSDB','TEMPDB','MODEL','DISTR
IBUTOR')
HTH
Jerry
"john wright" <riley_wright@.hotmail.com> wrote in message
news:%23jo41JN0FHA.2212@.TK2MSFTNGP15.phx.gbl...
> When I execute the sp_databases proc all I get are the databases that the
> logged in user has rights to. I want a stored procedure that will return
> just the names of all the non-system databases that I can populate a
> dropdown list with. I really don't want to add the user to every database
> or give them an Administrator role just to see the user table names. Any
> help on this would be much appreciated.
>
> John
>|||>> I want a stored procedure that will return just the names of all the
You can query the INFORMATION_SCHEMA.SCHEMATA view or the sysdatabases
system table to the the list of all databases. You can avoid the master,
model, msdb, mssqlweb, tempdb, Pubs & Northwind databases in the WHERE
clause to get the list of non-system databases.
Anith|||Great. This works just fine.
John
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:ulawJVN0FHA.3956@.TK2MSFTNGP09.phx.gbl...
> You can query the INFORMATION_SCHEMA.SCHEMATA view or the sysdatabases
> system table to the the list of all databases. You can avoid the master,
> model, msdb, mssqlweb, tempdb, Pubs & Northwind databases in the WHERE
> clause to get the list of non-system databases.
> --
> Anith
>sql
get a return value from an insert without using a stored proc.
hi all,
lets say i have this insert command being executed from C# to a SQL Db.
//store transaction logSqlCommand cmdStoreTrans =new SqlCommand("INSERT into Transactions(ImportID,ProfileID,RowID) values (@.ImportID,@.ProfileID,@.RowID);",conn);cmdStoreTrans.Parameters.Add("@.ImportID",importId);cmdStoreTrans.Parameters.Add("@.ProfileID",profileId);cmdStoreTrans.Parameters.Add("@.RowID",i);try{conn.Open();cmdStoreTrans.ExecuteNonQuery();conn.Close();}catch(SqlException ex){throw(ex);}
I need the new Identity number of that record added. how can i get that within THIS Sqlcommand. Currently im closing the connection, creating a new command with 'SELECT MAX(id) from transactions" and getting the value that way, im sure there is a easier way keeping it all within one command object? someone mentioned using something liek @.@.Identity
any help appreciated
TIA, mcm
Try doing a SELECT SCOPE_IDENTITY() at the end of your INSERT statement. and use ExecuteScalar instead of ExecuteNonQuery.|||thanks,
will do.
mcm
Tuesday, March 27, 2012
Get @@rowcount data from MSSQL using SqlDataSource with delete command
Hi
I'm using a simple SqlDataSource to connect to a stored proc to delete a number of rows from different tables.
In my SQL im using:
DECLARE @.table1CountintDELETE FROM Table1WHERE id = @.new_idSET @.table1Count=@.@.rowcountSELECT @.table1Count
I'm then using an input box and linking it to the delete control parameter. Then on a button click event i'm running SqlDataSource1.Delete() which all works fine. But how do i get the @.table1Count back into my aspx page?
Thanks
use OUT parameter
CREATE PROC dbo.SP_DeleteID(@.new_id int, @.effRowCnt intOUT)
As
begin
DELETE FROM Table1WHERE id = @.new_id
SET @.effRowCnt=@.@.rowcount
end
|||ok, but how do i reference the @.effRowCnt in the code behind page?
thanks
|||If you are just trying to get a count of the number of rows deleted, an easier way to do it is to use the Deleted methodof the DataSource, something like:
ProtectedSub SQLDataSource1_Deleted(ByVal senderAsObject,ByVal eAs System.Web.UI.WebControls.SqlDataSourceStatusEventArgs)Handles SQLDataSource1.Deleted
NumberofRowsDeleted = e.AffectedRows
EndSub
HTH
Friday, March 23, 2012
Generating unique nvarchar unique key
Would like to know whether it is possible to generate a unique key for a
nvarchar column. This key will need to generate by a T-SQL Stored Proc, the
format as follow:
XX-ZZZZ
XX is month, where I think it should be able to generate using DATEPART(mm,
GETDATE())
- is fixed delimiter
ZZZZ is a running number, which will be reseted to 0000 when it reach 9999
So, everytime we run the Stored Proc to generate this key, the statement
will need to break the ZZZZ and increase by one in order to generate the new
id (concatenate with XX-).
But we run into duplication key issue when this Stored Proc was called by
several concurrent clients (like every few thousand records, duplication of
keys will occur once, but occurance totally random).
Would like to know whether there are any better ways to ensure the key
generated (with above format) will always be unique?
ThanksIf you plan to use this as a primary key, it's a very bad idea. The best
primary key is an auto-incremented integer. MSSQL will manage everything for
you with an identity field. If you absolutely need that field you're talking
about (XX-ZZZZ), then create a second field and populate it with a process
that runs every hour or every few minutes. And most importantly, don't make
it part of the primary key.
What you are suggesting is an invitation for deadlocks, and a whole bunch of
other problems. For instance, are you 150% sure that you won't have more
than 10 000 records per month?
Alain Quesnel
alainsansspam@.logiquel.com
www.logiquel.com
"Thalia Mei" <thaliamei@.gmail.com> wrote in message
news:F165CCBF-5AC9-4BAF-B48F-477728F8A399@.microsoft.com...
> Hi,
> Would like to know whether it is possible to generate a unique key for a
> nvarchar column. This key will need to generate by a T-SQL Stored Proc,
> the format as follow:
> XX-ZZZZ
> XX is month, where I think it should be able to generate using
> DATEPART(mm, GETDATE())
> - is fixed delimiter
> ZZZZ is a running number, which will be reseted to 0000 when it reach 9999
> So, everytime we run the Stored Proc to generate this key, the statement
> will need to break the ZZZZ and increase by one in order to generate the
> new id (concatenate with XX-).
> But we run into duplication key issue when this Stored Proc was called by
> several concurrent clients (like every few thousand records, duplication
> of keys will occur once, but occurance totally random).
> Would like to know whether there are any better ways to ensure the key
> generated (with above format) will always be unique?
> Thanks
Monday, March 19, 2012
Generating an event from an Activation Stored Proc
I am trying to raise an event using sp_trace_generateevent in my activation stored proc. (dbo.ActivationSP)
EXEC sp_trace_generateevent @.event_class = 82, @.userinfo = N'Test Event'
There is a Service broker service listening to this event.
The problem is the event is getting fired whenever the activation SP is executed (could see in profiler) but the secondtargetqueue doesnt receive any messaages.
But if manually do a "EXEC sp_trace_generateevent", the secondtargetqueue receives a messaage.
Both the queues and sevices are in the same database.
The following are the code snippets
-- Code for queue on which the activation is working
CREATE QUEUE TargetQueue WITH STATUS=ON, ACTIVATION (PROCEDURE_NAME = dbo.ActivationSP,MAX_QUEUE_READERS = 5,Execute AS 'dbo') ;
Create Service ReceiverService ON QUEUE TargetQueue (SampleContract)
-- Code for Queue listening to event
Create Queue SecondTargetQueue WITH status= ON
Create Service SecondReceiverService ON QUEUE SecondTargetQueue ([http://schemas.microsoft.com/SQL/Notifications/PostEventNotification])
CREATE EVENT NOTIFICATION TestNotification
ON SERVER FOR UserConfigurable_0 TO SERVICE 'SecondReceiverService', 'current database'
Due to a product defect some profiler events are suppresed under activation. A possible workaround is to use a CLR activated procedure that connects back to the server (using ADO.Net and a real connection, not the context connection) to execute the code you need.
HTH,
~ Remus
Thanks Remus
Can you help me with some example links?
Generating an event from an Activation Stored Proc
I am trying to raise an event using sp_trace_generateevent in my activation stored proc. (dbo.ActivationSP)
EXEC sp_trace_generateevent @.event_class = 82, @.userinfo = N'Test Event'
There is a Service broker service listening to this event.
The problem is the event is getting fired whenever the activation SP is executed (could see in profiler) but the secondtargetqueue doesnt receive any messaages.
But if manually do a "EXEC sp_trace_generateevent", the secondtargetqueue receives a messaage.
Both the queues and sevices are in the same database.
The following are the code snippets
-- Code for queue on which the activation is working
CREATE QUEUE TargetQueue WITH STATUS=ON, ACTIVATION (PROCEDURE_NAME = dbo.ActivationSP,MAX_QUEUE_READERS = 5,Execute AS 'dbo') ;
Create Service ReceiverService ON QUEUE TargetQueue (SampleContract)
-- Code for Queue listening to event
Create Queue SecondTargetQueue WITH status= ON
Create Service SecondReceiverService ON QUEUE SecondTargetQueue ([http://schemas.microsoft.com/SQL/Notifications/PostEventNotification])
CREATE EVENT NOTIFICATION TestNotification
ON SERVER FOR UserConfigurable_0 TO SERVICE 'SecondReceiverService', 'current database'
Due to a product defect some profiler events are suppresed under activation. A possible workaround is to use a CLR activated procedure that connects back to the server (using ADO.Net and a real connection, not the context connection) to execute the code you need.
HTH,
~ Remus
Thanks Remus
Can you help me with some example links?
generating ALTER statement with SMO
Is it possible to generate ALTER statement for the stored proc with the SMO?
Any link will be appreciated. Thanks.
Any help please. Or it is not possible at all.|||In practice I always drop an existing stored procedure and recreate it when a change is required. Is there a particular reason why you'd rather alter the procedure?generating ALTER statement with SMO
Is it possible to generate ALTER statement for the stored proc with the SMO?
Any link will be appreciated. Thanks.
Any help please. Or it is not possible at all.|||In practice I always drop an existing stored procedure and recreate it when a change is required. Is there a particular reason why you'd rather alter the procedure?Monday, March 12, 2012
Generate view and stored proc scripts retaining depandancy order
We need to generate view and stored proc scripts so that they are
generated in the right order. eg
ViewA uses ViewB
Generate
--
ViewB
ViewA
etc
When using Enterprise Manager it just dumps the views and stored procs
alphabetically.
regards
PaulUsing EM, there is no guarantee that it'll generate the SP scripts in the
correct dependency order. To be sure, you need to sort then generated
scripts yourself.
I do this by using a script to scanning the SP scripts, find all the
immediate dependencies, and sort the SP by depdendency.
--
Linchi Shea
linchi_shea@.NOSPAMml.com
"Paul Sjoerdsma" <paul.sjoerdsma@.redsnapper.nl> wrote in message
news:MPG.19e266ada9d972919896c3@.127.0.0.1...
> Hi,
> We need to generate view and stored proc scripts so that they are
> generated in the right order. eg
> ViewA uses ViewB
> Generate
> --
> ViewB
> ViewA
> etc
> When using Enterprise Manager it just dumps the views and stored procs
> alphabetically.
> regards
> Paul
Wednesday, March 7, 2012
Generate Scripts
it's objects?
I'm using SQL 7.
Thanks-in-advanceNo built-in stored procedure available.
Check out http://www.mssqlcity.com/Scripts/T-...erateScript.sql
Why not use the Enterprise Manager?
Nathan H. Omukwenyi
"Bob" <Go1369@.Yahoo.Com> wrote in message
news:1147202443.779246.214780@.g10g2000cwb.googlegroups.com...
> Is there a stored proc to generate scripts to create a database and all
> it's objects?
> I'm using SQL 7.
> Thanks-in-advance
>|||You can use sp_OACreate and use the SQLDMO COM object where the scripting fu
nctionality exists. Do
some googling and I'm sure you will find just such samples.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Bob" <Go1369@.Yahoo.Com> wrote in message
news:1147202443.779246.214780@.g10g2000cwb.googlegroups.com...
> Is there a stored proc to generate scripts to create a database and all
> it's objects?
> I'm using SQL 7.
> Thanks-in-advance
>|||I want to be able to create a dts package or script because I would run
it every other year or so.|||How about some Active scripting using the DMO object model? See:
http://www.karaszi.com/SQLServer/in...rate_script.asp
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Bob" <Go1369@.Yahoo.Com> wrote in message
news:1147203928.872491.73660@.v46g2000cwv.googlegroups.com...
>I want to be able to create a dts package or script because I would run
> it every other year or so.
>