drop logins from SQL Server 2000 - sql-server-2000

I stacked here when I try to drop logins from sql server 2000
I used this script to get all the lists before dropping the logins, after I got all the lists I tried to execute the outputs, like "Drop login [xxxxx]" but got this error messages.
“Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'LOGIN'.”
The Script as follow
----------Script Begin--------
CREATE TABLE #sid (sid binary(85) NOT NULL)
INSERT #sid (sid)
EXEC sp_MSforeachdb 'select sid from sysusers WHERE sid IS NOT NULL'
SELECT 'DROP LOGIN ' + quotename(name)
FROM master.dbo.syslogins l
WHERE NOT EXISTS (SELECT *
FROM #sid s
WHERE s.sid = l.sid)
AND name NOT LIKE '##%'
AND name NOT LIKE 'NT %'
go
DROP TABLE #sid
----------Script ends--------
Please help me out
Am newbie for sql server
Any help would be much appreciated
Thanks

SQL 2000 code to drop a login:
EXEC master..sp_revokelogin #loginame='YOURDOMAIN\xxxxx'

This is what SSMS generated for SQL 2000. Funny thing is, there is different code for domain and SQL logins.
Domain login:
EXEC master.dbo.sp_revokelogin #loginame = N'your_domain\login'
SQL login:
EXEC master.dbo.sp_droplogin #loginame = N'sql_login'

Related

Delete Data from all tables in Sqlserver Database except some tables

I have A SQLSERVER Database, I want to Delete all tables except some tables
i use use this script
EXEC sp_MSforeachtable 'IF OBJECT_ID(''?'') NOT IN (
ISNULL(OBJECT_ID(''[dbo].[T1]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T2]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T3]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T4]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T5]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T6]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T7]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T8]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T9]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T10]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T11]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T12]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T13]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T14]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T15]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T16]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T17]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T18]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T19]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T20]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T21]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T22]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T23]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T24]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T25]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T26]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T27]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T28]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T29]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T30]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T31]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T32]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T33]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T34]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T35]''),0)
)
DELETE FROM ?'
the sqlserver return this error message "Msg 102, Level 15, State 1, Line 22
Incorrect syntax near 'ISN'."
I think the problem may be about the number of tables which is excepted
Try to do it another way:
DECLARE #command nvarchar(max);
--Remove spaces in front of ,ISNULL
SELECT #command = N'IF OBJECT_ID(''?'') NOT IN (
ISNULL(OBJECT_ID(''[dbo].[T1]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T2]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T3]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T4]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T5]''),0)
...
,ISNULL(OBJECT_ID(''[dbo].[TN]''),0)
)
DELETE FROM ?';
EXEC sp_MSforeachtable #command;
NOTE: This SP works only with nvarchar(2000) in first command (source).
It is the first command to be executed by this Stored Procedure and the data type is nvarchar(2000).
EXEC sp_MSforeachtable N'IF OBJECT_ID(''?'') NOT IN (ISNULL(OBJECT_ID(''[dbo].[T1]''),0),ISNULL(OBJECT_ID(''[dbo].[T2]''),0),ISNULL(OBJECT_ID(''[dbo].[T3]''),0),ISNULL(OBJECT_ID(''[dbo].[T4]''),0),ISNULL(OBJECT_ID(''[dbo].[T5]''),0),ISNULL(OBJECT_ID(''[dbo].[T6]''),0),ISNULL(OBJECT_ID(''[dbo].[T7]''),0),ISNULL(OBJECT_ID(''[dbo].[T8]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T9]''),0),ISNULL(OBJECT_ID(''[dbo].[T10]''),0),ISNULL(OBJECT_ID(''[dbo].[T11]''),0),ISNULL(OBJECT_ID(''[dbo].[T12]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T13]''),0) ,ISNULL(OBJECT_ID(''[dbo].[T14]''),0),ISNULL(OBJECT_ID(''[dbo].[T15]''),0),ISNULL(OBJECT_ID(''[dbo].[T16]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T17]''),0),ISNULL(OBJECT_ID(''[dbo].[T18]''),0),ISNULL(OBJECT_ID(''[dbo].[T19]''),0),ISNULL(OBJECT_ID(''[dbo].[T20]''),0),ISNULL(OBJECT_ID(''[dbo].[T21]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T22]''),0),ISNULL(OBJECT_ID(''[dbo].[T23]''),0),ISNULL(OBJECT_ID(''[dbo].[T24]''),0),ISNULL(OBJECT_ID(''[dbo].[T25]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T26]''),0),ISNULL(OBJECT_ID(''[dbo].[T27]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T28]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T29]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T30]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T31]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T32]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T33]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T34]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T35]''),0)
)
DELETE FROM ?'
The issue is that sp_msforeachtable has first parameter of length 2000 to which your query is passed. Since your query is of length more than that, full text of it is not passed to proc. That's why the error. Shorten the length of query by removing some line breaks. Try my query its working

Single line GO(Batch) statement giving error in SQL Server?

I have one question. I was creating below procedure temporary.
When I Execute it in below format it works fine:
CREATE PROCEDURE Get_TableList_By_Name
#Proc_Name VARCHAR(255)
AS
BEGIN
SELECT * FROM sys.tables WHERE name LIKE '%' + #Proc_Name + '%'
END
GO
EXEC Get_TableList_By_Name 'norway'
GO
DROP PROCEDURE Get_TableList_By_Name
GO
But when I execute same SQL in below format it giving error saying: "Incorrect syntax near 'GO'."
CREATE PROCEDURE Get_TableList_By_Name #Proc_Name VARCHAR(255) AS BEGIN SELECT * FROM sys.tables WHERE name LIKE '%' + #Proc_Name + '%' END GO EXEC Get_TableList_By_Name 'norway' GO DROP PROCEDURE Get_TableList_By_Name GO
CREATE PROCEDURE Get_TableList_By_Name #Proc_Name VARCHAR(255) AS BEGIN SELECT * FROM sys.tables WHERE name LIKE '%' + #Proc_Name + '%' END GO 1 EXEC Get_TableList_By_Name 'norway' GO 1 DROP PROCEDURE Get_TableList_By_Name GO 1
How to write same SQL with GO statement in single line?
Is it possible? If not then Why?
Thanks,
Vishal
From GO (Transact-SQL)
A Transact-SQL statement cannot occupy the same line as a GO command.
However, the line can contain comments.
So Go needs to be on its own line, except for comments.
'GO' is not a SQL command. It is a batch terminator recognized by tools
like Query Analyzer, SSMS, SQLCMD, etc. These tools generally require the
GO to be on a separate line and send the preceding SQL statements as a batch
to SQL Server when the GO is encountered
GO Statement must be written in new line as it is not T-SQL command.
T-SQL statement can not occupy the same line as GO. GO statement can contain comments.
You can execute this:
EXEC SP_COMMAND 1,2;
EXEC SP_COMMAND 2,2;
EXEC SP_COMMAND 3,2;
Set EXEC Bebore sp and ';' for end statement
then
select all code (SSMS) and press f5 o exec all you code by other thecnic.

check user account ad_SSmith does not have dbo on multiple database (SQL server 2008 R2)

I have about 100+ databases, and I want to check which database user ad_SSMith does not have dbo in. I ran EXEC sp_MSforeachdb to check which database he HAS dbo, but I would like to see which one he does not rather then doing a check off list and going back.
Is there away I can check all the databases to see in which ones the user does not have dbo?
Thank you so much.
How about something like this?
declare #sql varchar(281);
set #sql = 'use [?];
if exists (select 1 from sys.database_principals p
inner join sys.database_role_members r on p.principal_id = r.member_principal_id
where p.Name = ''domain\ad_SSMith''
and r.role_principal_id = 16384)
select DB_NAME(), ''db_owner''
else
select DB_NAME(), ''Not db_owner'''
exec sp_msforeachdb #sql;

Find a database with a particular table OR Find a table in every database of SQL Server

I have a SQL Server with hundreds of databases and each database having hundreds of tables.
Now I would like to find where in these databases is a table that I am looking for.
I could find if a table existed in individual database using
use myDatabase
select * from sys.tables where name = 'mytable'
GO
but using this means I have to manually change the database for hundreds of times .
I would like to find the database name only.
Is there a way out ?
Okay, if you're just wanting to find each database that contains a particular table, and aren't going to be querying the table, then you can just do:
create table #t (
DBName sysname not null
)
go
exec sp_MSforeachdb 'use [?]; if OBJECT_ID(''dbo.mytable'') is not null insert into #t (DBName) select ''?'''
go
select * from #t
go
drop table #t
(If you're not using multiple schemas in your databases, you won't need to specify dbo in the OBJECT_ID call, otherwise I use it to avoid finding tables in the wrong schema)
This should do what you are looking for:
EXEC sp_MSforeachdb "use [?];select * from sys.tables where name='TableName' "
To include the name of the current database in the output use:
EXEC sp_MSforeachdb "use [?];select '[?]' as DatabaseName, * from sys.tables where name='TableName' "
SELECT DISTINCT DB_NAME(database_id)
FROM [sys].[dm_db_index_operational_stats](NULL,NULL,NULL,NULL)
WHERE OBJECT_NAME(object_id,database_id) = 'mytable'
I know this is an old thread but was high on my google search. So I wanted to contribute for others looking to find a database with a certain table in it. These apply to SQL Server 2008 - Current.
I started with this, which worked for my SA level login, but gave me issues with users that did not have permissions to all databases.
SELECT name
FROM sys.databases
WHERE CASE
WHEN state_desc = 'ONLINE' THEN OBJECT_ID( QUOTENAME( name ) + '.[dbo].[mytablename]','U' )
END IS NOT NULL;
But ended up with this adding the HAS_DBACCESS(name) = 1 in restriction so that the query would not fail with a security error.
SELECT name
FROM sys.databases
WHERE HAS_DBACCESS(name) = 1 and
CASE
WHEN state_desc = 'ONLINE' THEN OBJECT_ID( QUOTENAME( name ) + '.[dbo].[mytablename]','U' )
END IS NOT NULL;
exec sp_msforeachdb #command1='
USE ?;
select * from sys.tables where name = ''CLIENTS'''
this is also one of the way, similar with solution of #Jonathan :
exec sp_MSforeachdb 'SELECT "?" AS DB, * FROM [?].sys.tables WHERE name like ''%YourTableName%'''
exec 'select ''?'', name from [?].sys.tables where name = ''yourTable'''

SQL CREATE LOGON - can't use #parameter as username

I'm a developer and I suck at SQL:) Please help me out here.
I'd like to create my own Stored Procedure that creates a Tenant in my SaaS database. In order to do this I need to create a new SQL Login for the Tenant and then add it to a predefined SQL Role.
I'm already stumped just trying to create the Login. Here is what I've tried...
CREATE PROCEDURE [MyScheme].[Tenants_InsertTenant]
#username nvarchar(2048),
#password nvarchar(2048)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
CREATE LOGIN #username WITH PASSWORD = #password
END
Msg 102, Level 15, State 1, Procedure Tenants_InsertTenant, Line 16
Incorrect syntax near '#username'.
Msg 319, Level 15, State 1, Procedure Tenants_InsertTenant, Line 16
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
I realize this should be straightforward but when your new to SQL and the SQL manager errors are as cryptic as they seem to be to me its better just to ask for help:)
Thanks,
Justin
Apparently CREATE LOGIN only accepts literals.
You could try wrapping it in an exec and building it as a string:
EXEC('CREATE LOGIN ' + quotename(#username) + ' WITH PASSWORD = ' + quotename(#password, ''''))
edit: added quotename for safety from sql injection attacks
Posible solution:
sp_addlogin #loginame = 'test', #passwd = 'test', #defdb = 'test'
Try this:
declare #t nvarchar(4000)
set #t = N'CREATE LOGIN ''''' + #username + ''''' WITH PASSWORD = ''''' + #password
exec sys.sp_executesql #t
Building on the answers from #codeulike and #Galkin I ended up doing this:
DECLARE #t nvarchar(4000)
SET #t = N'CREATE LOGIN ' + QUOTENAME(#username) + ' WITH PASSWORD = ' + QUOTENAME(#password, '''') + ', default_database = ' + QUOTENAME(#DatabaseName)
EXEC(#t)
I'm running SQL Server 2019 and combining EXEC(--> with QUOTENAME() inside <--) on the same line does not work.
If I understand the microsoft sql injection documentation using quotename to build a string then executing protects you from SQL injections.