Primavera switching computers master.mdf replace not working - sql

I'm using primavera with a SQL Server database.
I switched computers, so I took with me the primavera_DAT, primavera_LOG files, then I installed primavera on the other computer and replaced the above files.
The issue is, whenever I enter primavera, I can establish the connection, but can not login and it is telling me the username and password are wrong.
I did some research and I found out that the usernames and passwords are stored in the master.mdf file.
I also have the master.mdf from the other computer, however when I replace them, I am getting the error whenever I start the SQL Server service from services.
How can I recreate the username or pass for the users (privuser, pubuser) that is, get the new master.mdf like the old one ??

You can recreate the necessary logins from the users of your database using sp_change_users_login with 'Auto_fix' which will map existing logins and create new logins if they don't exist. The password has to be given and changed later for not existing logins.
Declare #Membername varchar(255)
Declare #SQl Varchar(8000)
Select #SQL=''
Create Table #tmp
(
DbRole varchar (255),
Membername varchar (255),
MemberSid uniqueIdentifier
)
insert into #tmp exec sp_helprolemember
Delete from #tmp where Membername in ('sa','dbo','guest')
Delete from #tmp where Membername like ('db_%')
DECLARE P_cursor CURSOR FOR
SELECT Distinct Membername from #tmp
OPEN P_cursor
FETCH NEXT FROM P_cursor
INTO #Membername
WHILE ##FETCH_STATUS = 0
BEGIN
Select #SQL=#SQL + 'exec sp_change_users_login ''Auto_Fix'', ''' + #Membername +''' , NULL, '+'''B3r12-3x$098f6''' +Char(13)+Char(10)
FETCH NEXT FROM P_cursor
INTO #Membername
END
CLOSE P_cursor
DEALLOCATE P_cursor
Drop Table #tmp
--Print #SQL
Exec (#SQL)

Related

Running SQL query for common table in all databases

I'm trying to run SQL query to populate result against a common table in all databases.. however it is resulting in false output
Create table #userid (dbname varchar(50), username varchar(50))
EXEC sp_MSforeachdb 'IF ''?'' NOT IN (''master'',''security'',''tempdb'')
BEGIN
inset into #userid
select ''?'' dbname, username from zuserdb where username IN (''a1234'',''b1234'')
END'
Select * from #userid
DROP table #userid
Above Code isn't generating right output .. multiple rows are being generated however user actually doesn't exist in database. Any suggestions
Not sure why that is happening it is undocumented, but here is one workaround.
Use three part identifier in table name. Considering zuserdb is present in dbo schema in all the database.
CREATE TABLE #userid
(
dbname VARCHAR(50),
username VARCHAR(50)
)
EXEC Sp_msforeachdb
'IF ''?'' NOT IN (''master'',''security'',''tempdb'',''model'',''msdb'')
BEGIN
insert into #userid
select ''?'' dbname, username from ?.dbo.zuserdb where username IN (''a1234'',''b1234'')
END'
SELECT *
FROM #userid
DROP TABLE #userid
Added model & msdb databases also in exclude list because those are also system databases

Loop for multiple database and user creation in SQL, assign user to the database

after creating login using windows authentication ,
need to assign user to the database
and provide permissions to that user.
Could anyone please help me with that.
Thanks in advance.
-- BULK INSERT tempNames.dbo.tempNames
-- FROM 'C:\Users\Videos\file.txt'
-- WITH
-- (
-- ROWTERMINATOR ='\n'--
-- )
USE [master]
GO
DECLARE #NameCursor as CURSOR;
DECLARE #NAME AS NVARCHAR(50);
DECLARE #NAME2 AS NVARCHAR(50);
DECLARE #NIUNT AS NVARCHAR(50);
SET #NIUNT ='niunt';
SET #NameCursor = CURSOR FOR
SELECT id
FROM test.dbo.Sheet1$
OPEN #NameCursor;
FETCH NEXT FROM #NameCursor INTO #Name;
WHILE ##FETCH_STATUS = 0
BEGIN
PRINT #Name
Creating database using #name
set #name2 ='create database '+#Name + ';'
exec (#name2)
BEGIN
SET NOCOUNT ON
DECLARE #SQL NVARCHAR(4000);
Creating login using #name
SET #SQL = 'CREATE LOGIN [' +#NIUNT +'\'+ #NAME + '] from windows';
exec(#SQL);
-END;
FETCH NEXT FROM #NameCursor INTO #Name;
END
GO
This is awfully intricate. It seems easier to operate directly upon the underlying system tables that actually represent the authentication and authorization data. Then you won't have to muck about with cursors and such.
FOR EXAMPLE, let's say that logins were represented by a table, SYS$LOGINS, with fields SYS$USERNAME and SYS$PASSWORD. Isn't it easier to just INSERT a record for user 'joe' into that table (let's not worry about the details of how encrypted passwords are stored: it's just a quick example!) than to have to define cursors, and construct SQL strings, and exec() them?
I should think you have to be the SYSTEM user to do all this, but I'm sure you already are.

How to keep client databases consistent whenever a coder changes stored proc/table definition/views/triggers etc

I have asked this question before (here), but it never solved my problems.
Here is the scenario:
1. A coder modifies a stored proc/table definition/views etc on his "development server"
2. The modified T-SQL code is tested and passed by another team
3. Now the tested T-SQL code needs to be updated in 20 client databases. (Which is an extremely tough task).
4. Currently, we copy paste the T-SQL code in every db individually. This also results in errors which are resolved only when the client complaints.
We are using SQL Server 2012, and I guess usage of Schema's may resolve this issue. But I don't know how to do it.
Probably you can use the bellow query. Only thing is you must have access to all the databases and all those DBs are in the same server.
-- Provide DB names as CSV
DECLARE #DBNames VARCHAR(MAX) = 'ExpDB,ExpDB_DUP'
-- Provide Your Update Script here
DECLARE #Script VARCHAR(MAX) = 'CREATE TABLE TestTab (Id int IDENTITY(1,1) NOT NULL,
Value nvarchar(50) NULL)'
DECLARE #DBNamesTab TABLE (DBName VARCHAR(128))
INSERT INTO #DBNamesTab
SELECT LTRIM(RTRIM(m.n.value('.[1]','varchar(128)'))) AS DBName
FROM
(
SELECT CAST( '<XMLRoot><RowData>'
+ REPLACE(#DBNames,',','</RowData><RowData>')
+ '</RowData></XMLRoot>' AS XML) AS x
)t
CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)
DECLARE #DBName VARCHAR(128)
DECLARE #ScriptExe VARCHAR(MAX)
DECLARE dbNameCursor CURSOR FOR SELECT DBName FROM #DBNamesTab
OPEN dbNameCursor
FETCH NEXT FROM dbNameCursor INTO #DBName
WHILE ##FETCH_STATUS = 0
BEGIN
SET #ScriptExe = 'USE ' + #DBName + ' ' + #Script
EXEC(#ScriptExe)
FETCH NEXT FROM dbNameCursor INTO #DBName
END
CLOSE dbNameCursor;
DEALLOCATE dbNameCursor;

How to rename users, add a role etc, using script in t-sql

I have to
Rename users (from yyyyyy\xxxx to xxxx)
Add a role to the users
See the priviliges of stored procedures granted to a specified role (I found a table with the information regarding tables, but not stored procedure)
All in t-sql. I know how to do it mannualy, but with 400+ users, I hope to script me out of the problems.
Can anyone help?
What you need to do is loop over the users to modify and execute the commands to make the changes you need. You can do this by querying the syslogins table and creating a cursor with the results.
I have added the statement to rename the user, but adding the role is as simple as adding in a second statement and exec with sp_addrolemember
DECLARE #Login as varchar(50);
DECLARE LoginsCrsr CURSOR FOR
SELECT name
FROM syslogins
WHERE name like '%John%'; --Whatever critera you need
OPEN LoginsCrsr;
FETCH NEXT FROM LoginsCrsr
INTO #Login;
WHILE (##FETCH_STATUS = 0)
BEGIN
DECLARE #TSQL as varchar(255)
DECLARE #NewLogin as varchar(50)
SELECT #NewLogin = #Login -- Do your own thing here
SELECT #TSQL = 'ALTER LOGIN [' + #Login + '] WITH NAME=[' + #NewLogin + ']'
PRINT #TSQL
EXEC (#TSQL)
--Whatever else you need to do
FETCH NEXT FROM LoginsCrsr
INTO #Login
END
GO
CLOSE LoginsCrsr;
DEALLOCATE LoginsCrsr;
GO

Grant SELECT, UPDATE, INSERT, DELETE to all tables except 1 (or more) in SQL Server 2005

The user I have should have access to all tables in a database - SELECT, INSERT, UPDATE, DELETE and EXECUTE (ASP code to blame :-P) except for 1 table e.g. users.
When granting db_datareader and db_datawriter this gives them full access to everything and removing the DELETE permission on the users table will not work.
There are over 60 tables and was looking for a quicker way than using SSMS to go through every table and do this.
How would I go about doing this?
You can explicitly deny permissions which should take precedence. The syntax is
deny delete on dbo.users to username
You can use a hacky cursor and sp_executeSQL (GRANT can't take a variable tablename)
declare ctable cursor for
select Name from sysobjects where type = 'u'
declare #Name sysname
declare #ExecSQL nvarchar(512)
open ctable
fetch next FROM ctable into #Name
while (##FETCH_STATUS = 0)
begin
if (#Name <> 'MyTableToExclude')
BEGIN
SET #ExecSQL = 'grant SELECT on ' + #Name + ' to PUBLIC'
EXEC sp_executesql #ExecSQL
... etc
END
fetch next FROM ctable into #Name
end
close ctable
deallocate ctable
Minor ... note that you can't grant exec on tables ;)