Transact SQL :: Log All Changes In Database?
Sep 15, 2015I want to find the best way to log all changes in my database. for example who changes which field and when it has been done.
View 6 RepliesI want to find the best way to log all changes in my database. for example who changes which field and when it has been done.
View 6 RepliesI'm trying to figure out how to identify the calling database within a sproc on a different database without using a parameter.
View 2 Replies View RelatedAre there other ways to change the database ?
For instance command stringdatabasename ?
Hello,I encounter a problem with a small portion of sqlcode. I try to go ondatabase using "use dbname" but i always stay in master. I executescript with the sa user.declare @dbnamesysnamedeclare @ret_codeintDECLARE db_cursor CURSOR FORselectnamefrommaster..sysdatabaseswherename not in ('master', 'model', 'tempdb', 'pubs', 'Northwind')-- Open cursorOPEN db_cursorFETCH NEXT FROM db_cursor INTO @dbnameWHILE @@FETCH_STATUS = 0BEGINexecute ('use ' + @dbname)execute ('select db_name()')Thank's for help,Pierrot.
View 2 Replies View RelatedI have looked around quite a bit, but mostly what I have found is looking to see if a table is used or if a column is in a stored procedure and honestly most of what I have seen does not work.
I want to reduce our nightly import by removing any columns that are not being used. We insert into our staging tables, Stage1 for example. And say Stage1 has column1 and column2. If those columns are not being used, then I want to remove them from Stage1. The only catch is that every Stage1 table has a v_Stage1. v_Stage1 should have all the columns from Stage1, but doesn't always. So I need to know what columns from Stage1 are used somewhere other than v_Stage1 and what columns from v_Stage1 are not used.
SQL Server version: 2012 (11.0.5592) Standard
I have a script that runs the following SQL in order to attach a database.
if db_id('$(db_name)') IS NULL
BEGIN
CREATE DATABASE [$(db_name)] ON
(FILENAME = $(mdf_path)),
(FILENAME = $(ldf_path))
FOR ATTACH;
END
When I run the script, it executes without error. If I check the Windows Event log i see messages about database recovery and then finally 'Starting up database 'mydb''. Now when I open Management Studio the database is not there.
If I run the script again, and then refresh in Management Studio, the database now shows up. I've tried not running the script at all and just manually attaching the database via Management Studio. I see the same messages in the Event log but this time, the database shows in Management Studio immediately. So my question is, why do I have to run the script twice? I've tried waiting for 10 mins and then hitting refresh in Management Studio but the db never appears until i rerun the script.
No error is thrown, but the update is not made? Possibly due to the dreaded inline sql being used? The data structure for these 2 servers is horrific (but that is a story in itself). The current structure (which needs immediate change) is each employee all 10 of them, have their own database. If the userid is 6 or higher they are on server2 if the userid is 5 or below they are on server1. Then they each have their own table that stores the data. (A story for another day, but def needs overhaul). However, here is the sql -- what needs to be altered to work with the current data structure and still get the updates needed? Or we can scratch the update statements entirely if we can get the correct counts needed.
Declare
@employeeID varchar(50)
,@managerID varchar(50)
,@sql varchar(Max)
,@employee varchar(max)
,@itemsold varchar(max)
[Code] ....
I have below database already on one of the environment and its surprisingly designed somewhat in the past.now I want to correct it with one default filegroup with one primary and one log file, same time i am concerned for data as its production and no test environment is there, any way which ensure full consistency and steps i need to do...
CREATE
DATABASE [Sample]
ON
PRIMARY
(
NAME =
N'Sample_Data',
[code]....
I am trying to run a SQL query from a Different Datawarehouse DB on the same server but getting error:
Msg 208, Level 16, State 1, Line 3
Invalid object name 'VirtualManagerDB.dbo.tbl_Cloud_CloudCapacity'.
My query is
select VM1.ID, VM1.VirtualCPUCount, VM1.Memory, VM1.Storage
from [VirtualManagerDB].[dbo].[tbl_Cloud_CloudCapacity] AS VM1
Am running above query in a different database: OperationsManagerDW
I have 2 database, databaseA and DatabaseB with same table structure.
I change lot of table structure in databaseB.
So i want compare what field that i add or delete in databaseB.
it's like what field there are in databaseB and not found in databaseA or nothing in databaseB but exist in databaseA.
How can i do that?
I am looking for a query that can search an entire database and return max length of all nvarchars in a database. I can write a cursor but its a pretty big database with lots and lots of columns using nvarchars with most of them having a length of around 300 and doing a select max(len(nvarchar_col) from table from few tables return only 12 or 13. So I want to restrict the length of this data type accordingly.
View 9 Replies View RelatedI have a Java application (APP) that use SQL Server 2008 - 20014. In the SQL server there can be more than my database. During setup of the database the first user (ex:ADM) is created using MS SQL Server Management Studio (SMS). ADM can then launch the APP with proper credentials and connect to the database.ADM can then, in the APP, create new users for the APP and database. After some testing for proper CREATE and GRANTS I use the following code to create a new Create, Read, Update, Delete (CRUD) user for the APP and database: CREATE LOGIN testuser WITH PASSWORD='password', DEFAULT_DATABASE = testdb
USE testdb
CREATE USER testuser FOR LOGIN testuser
GRANT AUTHENTICATE TO testuser WITH GRANT OPTION
GRANT CONNECT TO testuser WITH GRANT OPTION
GRANT ALTER ANY USER TO testuser WITH GRANT OPTION
GRANT CONNECT SQL TO testuser WITH GRANT OPTION
GRANT INSERT,SELECT,UPDATE, EXECUTE, DELETE TO testuser WITH GRANT OPTION
USE master
GRANT CONTROL SERVER TO testuser
USE testdb..When I the use the SMS and look at security the "testuser" is only added to testdb. To test I create a second database "testdb2" --> launch the APP to connect to testdb2 and can login with "testuser".I don't know if I give to much grants or missing one...proper TSQL so new users only have CRUD access to "testdb" and no other database on the SQL server.
i have a data base sql Server .I wrote a stored procedure or attach my database but it is attached in read only mode how can remove read-only.
this is my stored procedure.
create procedure attache
as
declare @trouvemdf int
declare @trouveldf int
if exists (select name from sysdatabases where name='Gestion_Parc')
[Code] ....
i try this EXEC sp_dboption 'Gestion_Parc', 'read only', 'FALSE' but it causes those error
Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file "C: Gestion_Parc.mdf". Operating system error 5: "5 (Access is denied.)".
Msg 5120, Level 16, State 101, Line 1
Unable to open the physical file "C: Gestion_Parc_log.ldf". Operating system error 5: "5 (Access is denied.)".
Msg 945, Level 14, State 2, Line 1
[Code] ....
suppose if we do not have FK then what kind of advantage we could not avail. we can fetch data from two table by creating a relation in sql.....then why FK is required.
View 10 Replies View RelatedIm working on Partition purge process, where I need to specify following statement:
SET @cmd = 'ALTER PARTITION FUNCTION ' + @function_name + '() MERGE RANGE (@range)'
EXEC (@cmd);
SET @cmd1 = 'ALTER DATABASE '+ db_name()+ ' REMOVE FILE ' + @partition_file
EXEC (cmd1);
I want to put this statement in Begin Tran /Commit statement but getting error that it is not allowed. "ALTER DATABASE statement not allowed within multi-statement transaction"..what are my options to rollback in case there is a failure.
In my procedure I need to pull data from a database which is on other server and domain.
As usual I have used the below query but having troubles with mode of authentication.
SELECT * FROM OPENROWSET('SQLNCLI', 'Server=x.x.x.xsyz;Trusted_Connection=yes;','SELECT top 10 * FROM pcm.stats.visits') AS a
I get the below error:
"Login failed. The login is from an untrusted domain and cannot be used with Windows authentication"
I understand from the error that the domain and mode of authentication is different.
I use windows auth and we are given sql auth on the remote server which is on different domain.
I was suggested to use 'sp_addlinkserver' to link the remote server and query the data.
I am just curious to know if we can succeed through link server, is there any script to use sql authentication of remote server in my procedure and pull the data.
surface some data stored in an Access database via SQL Server, ultimately to be consumed by SharePoint.
I understand how to push existing tables to SQL Server as new tables. In my case, what I am hoping to do is to link an Access query with a table in SQL Server, and have that table be updated each time the query is run (or even each time the database is updated).
As a part of DBA, I used to execute various SQL files. Most of the time, it is like a manual effort to execute the files individually.
I am looking to automate the process, like a single click to execute all the .SQL files.
The main hurdle I have is, some files needs to be executed in A1 database, some in B1 database and some other SQL files need to be executed in C1 database. In this scenario, I need to pass the DBName information to the powershell query dynamically.
My design for this requirement is, say each .SQL file need to contain a template like
@DBName = 'your Database name'
@Executeon = 'When to execute'
In this case, the powershell first need to read the SQL file and finds the value for @DBName and replace it in the powershell query and execute the SQL files automatically.
Is it feasible ? Or any other alternate easier way to proceed.
I linked MS SQL and MySQL and i would like to have insert trigger on MySQL to import certain fields every time when there's new entry.
View 5 Replies View RelatedI can use Profiler to see database usage activity. However, in addition to it, is there a good query I can use to see whether user databases are being used (last select, last update, last alter or last delete etc., with date/time stamp)?I am looking for both SQL2000 and SQL2005 as we need to decommission some of the older servers.
View 6 Replies View RelatedIs it possible to write query from different database engines?
One database A is in local server database engine and another database B is in another server located in different system.
Now , I need to write a query in database A based on table A and table B (another database engine).
I need to build a query to calculate the size of all tables in a database ...
View 5 Replies View RelatedI am using SQL 2012 SE. I am trying to move .mdf and .ndf files after a database is detached. Here is my code that is just to copy the mdf file. I am testing it against this file now and if it worked then I would do the move ldf file the same way.
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE
DECLARE @cmd nvarchar(4000)
[Code] ...
The only message is I see while the query is executing is :Configuration option 'show advanced options' changed from 0 to 1. Run the RECONFIGURE statement to install.
If I manually copy over the file it takes 30 seconds since the file is only 2GB and the script takes 45 minutes and still executing.
Trying to find tsql for TRIGGER which will send me email, if someone rename database in test/dev environment. I found it for CREATE and DROP database trigger but could not find for RENAME database.
View 8 Replies View RelatedI need to pull dates from a DB2 database via TSQL (Linked server - IBM DB2 for i IBMDASQL OLE DB Provider) and compare it to today for a less than or greater than type comparison.
Database: DB2, Customer information housed here
Columns:
UTOFMM - Month (2 character, numeric)
UTOFDD - Day (2 character, numeric)
UTOFYY - Year (2 character, numeric. Problem: years from 2000 to 2009 are stored as 0, 1, 2, ... etc)
UTOFCV - Century Value (2 char, numeric. 0 = before 2000, 1 = in or after 2000)
I need to concatenate the date to be "sql" friendly, and then compare to today's date. It's to find any customer with date values in the fields above, and then differentiate between dates before today and after today.Here is the snippet of what I'm trying to fix. This portion of a nightly job is just checking for <u>any</u> value in the UTOFMM column of the current record.
Add Customer ID
Update [responder].[Temp_RX_CUSTOMERS]
set CustomerID = lf.UTCSID
from [responder].[Temp_RX_CUSTOMERS] LEFT Outer Join
[HTEDTA].[THOR].[HTEDTA].UT210AP lf ON [responder].[Temp_RX_CUSTOMERS].LocationID = lf.UTLCID
where lf.UTOFMM = 0
GO
I have a created a snapshot and I'd like to know if there is a way for all the existing and future queries to refer to the database snapshot. Is there any way to do this?
View 4 Replies View RelatedTransfer tables with Data from one database to another one on a same server?
View 10 Replies View RelatedI have noticed rather strange behaviour of EXECUTE AS and REVERT sequence during the cross database calls which appear to be a bug. I tested this issue on developer edition of SQL Server 2012
Microsoft SQL Server 2012 - 11.0.5343.0 (X64)
May 4 2015 19:11:32
Copyright (c) Microsoft Corporation
Developer Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1)
This issue causes problems in SSISDB where similar piece of code appears in [catalog].[start_execution] and some other scripts in the [internal] schema. This was previous discussed in [URL] The following script illustrates the issue:
USE [master]
GO
CREATE DATABASE [Test1]
GO
CREATE DATABASE [Test2]
GO
-- Set Database to Trustworthy to allow cross database connection
ALTER DATABASE [Test1] SET TRUSTWORTHY ON;
GO
ALTER DATABASE [Test2] SET TRUSTWORTHY ON;
GO
USE [Test2]
GO
CREATE PROCEDURE [dbo].[TestContext]
AS
SELECT 'EXECUTION CONTEXT BEFORE EXECUTE AS CALLER', SUSER_NAME(), USER_NAME();
[code]....
I want to create database role which can be used for creating stored procedures like db_SPCreate.
User should be able to create SP on Schema PVR; and should not be allowed to create sP in otherthan schema and he can use any schema tables to create procedure.
[URL]
How do I get an accurate count of workstations connected to a database?
View 7 Replies View RelatedCan anyone explaing briefly, why would this error appears???
Thank you
Script only displays the result for 'Master' database.
What changes are required for script to display result for all databases on the instance?
SELECT DB_Name() AS DatabaseName, OBJECT_NAME(ind.OBJECT_ID) AS TableName,
ind.name AS IndexName, indexstats.index_type_desc AS IndexType,
indexstats.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) indexstats
INNER JOIN sys.indexes ind
ON ind.object_id = indexstats.object_id
AND ind.index_id = indexstats.index_id
WHERE indexstats.avg_fragmentation_in_percent > 30 and indexstats.page_count >1000
ORDER BY indexstats.avg_fragmentation_in_percent DESC
When I type Select statemet like below, Tables names are not populate auto. I need to type full lenght instead of selection in list.
Ex: I have few tables and database in my schema and need to select while coding.
select * from (Table Name) When i type first char in the table space it will not populate list of tables in my sql studio.