How I can find out the changes happened in database like modifying functions, table indexes, procedures and adding or removing columns.
Here in this query
select * from sys.objects
where type IS NOT NULL
and modify_date between '2013-07-21' and '2013-07-29'
but here I am getting created objects list and modifying list, but if I deleted any object it is not showing anything.
How can I get the all the changes happened in database between specific dates?
Try a source control solution for SQL. I've used RedGate's SQL Source Control before, and it records a history of changes like this, including who made the change, and what was changed.
http://www.red-gate.com/products/sql-development/sql-source-control/
It's a bit expensive, but it's good. I don't know if there's a way to do it (especially deletions) just with SQL itself.
Many of these incidents are recorded in the default trace.
DECLARE #path NVARCHAR(260);
SELECT
#path = REVERSE(SUBSTRING(REVERSE([path]),
CHARINDEX(CHAR(92), REVERSE([path])), 260)) + N'log.trc'
FROM sys.traces
WHERE is_default = 1;
SELECT
LoginName,
HostName,
StartTime,
ObjectName,
TextData -- may or may not be populated
FROM sys.fn_trace_gettable(#path, DEFAULT)
WHERE EventClass IN
(
164, -- object:altered
46, -- object:created
47 -- object:deleted
)
AND StartTime >= '20130721' AND StartTime < '20130730';
Why you should never use BETWEEN for date range queries.
Related
Today is, 1/30/2022, I have been following along with an #AlexTheAnalyst video. I am on a Mac and using MySQL version 8.0.27. (The video is using windows based SQL Server Workbench) I am stuck! I am trying to creating a temporary table function. MySQL is not liking the # in the table name #PercentPopVaccinated as used in the video. When I remove it and run the function/query without the # I get 0 rows returned. I have researched on stackoverflow etc. and I am not coming up with a solution that I understand. (Newbie here)
I have dropped the table that was created and I am starting over.
I am getting an error when creating the temp table that states MySQL is expecting an identifier after the first " ( ". Anyone else have a similar issue?
Create Table #PercentPopulationVaccinated
(
continent nvarchar(255),
location nvarchar(255),
date datetime,
population numeric,
new_vaccinations numeric,
RollingVacCount numeric
)
Insert into #PercentPopulationVaccinated
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
, SUM(cast(vac.new_vaccinations as UNSIGNED)) OVER (Partition by dea.location Order by dea.location, dea.date)
as RollingVacCount
-- (RollingVacCount/population)*100
From project_portfolio.covid_deaths dea
Join project_portfolio.covid_vaccinations vac
On dea.location = vac.location
and dea.date = vac.date
where dea.continent is not null
-- order by 2,3
Select *, (RollingVacCount/Population)*100
From #PercentPopulationVaccinated;
So I'd say the underlying problem is that you are watching a video tutorial that is using SQL Server, but you are using MySQL. There are many similarities, but it is not going to be an exact match. For instance, the # sign creates a temporary table in Sql Server, but the # is not valid in MySQL. If you want to use a different database service than the tutorial you are watching is for, you are going to have to translate some concepts for yourself.
Another commenter already posted this link, which indicates the syntax for creating temp tables in MySQL.
https://dev.mysql.com/doc/refman/8.0/en/create-table.html#create-table-temporary-tables
When using the below code the VS 2019 crashed without fail. Though it was originally working, possibly prior to a recent update (or upgrade to VS 2019 from VS 2017)
The error message implied a too long identifier and related to a specific file. I temp resolved this by accessing my project via Team Explorer as opposed to startup window. It resurfaced as soon as I edited the file. So I went line-by-line to find the culprit, the code below is the culprit. I cannot understand why it causes the crash.
DECLARE #Characteristics nvarchar(4000) = (SELECT * FROM OPENJSON(#Details)
WITH ([Firstname] nvarchar(256) N'$.firstname',[Lastname] nvarchar(256) N'$.lastname') FOR JSON PATH, WITHOUT_ARRAY_WRAPPER);
It's a legitimate command and SQL accepts including deployments. For my purposes and because I control the WITH clause, "SELECT *", is an acceptable deviation from my practice of stating the columns in the SELECT clause.
VS no longer crashes now that I declare each expected column in the SELECT clause. If somebody has an alternative resolution to solving this problem I would appreciate it.
You do not show enough - at least for me - to understand your issue completely...
What I get:
You want to use SELECT *
You are controlling the WITH clause (I assume: You build it dynamically in your application)
You did not show the actual error message (please do this the next time), but I get, that the error depends on the difference of
SELECT * FROM ...
SELECT Firstname, Lastname FROM ...
I cannot reproduce your issue, but I guess, that there are more sources involved in your actual statement and that * might include more than you tell us.
Did you try to use a table alias after the WITH?
DECLARE #Details NVARCHAR(MAX)=N'[{"a":"a1", "b":"b1"},{"a":"a2","b":"b2"}]';
SELECT
(
SELECT tbl.* --<-- Using "tbl.*" instead of "*"
FROM OPENJSON(#Details)
WITH(a NVARCHAR(100)
,b NVARCHAR(100)) tbl --<-- table alias "tbl"
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
);
So i do a simple query like so:
connection.query("UPDATE workers SET timestamp='"+thedate+"' WHERE id = " + id,function(err,upres){
connection.release();
if(!err) {
console.log('updated record')
console.log(upres);
}
})
console.log reveals the data format as: 2015-04-02 19:29:14
And if i debug the SQL statement, that turns out to be:
UPDATE workers SET timestamp='2015-04-02 21:31:16' WHERE id = 3;
However, when i list the data, the output is:
[{"id":3,"worker":"John Doe","timestamp":"2015-04-01T22:00:00.000Z","duration":30}]
This is way off compared to the time that is being reported?
What is causing this?
You do not know how MySQL is turning your VARCHAR into a date. There are a lot of configuration options. It would be better to use the STR_TO_DATE function to circumvent all of the assumptions. Here is a link to the docs for STR_TO_DATE().
As a side note, I would strongly recommend using prepared statements as a way to safeguard your application against errors and sql injection.
EDITS:
In regards to your questions, the column could be DATETIME, but your value you are assigning is a VARCHAR
'UPDATE workers SET timestamp = ? WHERE id = ?', ['4/2/2015 3:00:00 PM', 3'], [callBackFunction]
Based on what you said about the conversion not working, I am suspicious about the data type for the timestamp column.
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE NAME = 'workers'
A statement like that should give you all of the information about that column. You could also find this in a GUI, if you have access. There are three different date types in MySQL date, datetime, or timestamp. This is most likely a DATE column, that will not be able to hold the time.
I've added and modified several (new and existing resp.) tables and stored procs, for a particular database and server, in last 3 months.
I was thinking if there's any SQL query by which I can determine all those changes.
Thanks.
Query the sys.objects table to find the objects that changed and filter by modify_date and type; U = User table, P = Stored procedure.
select *
from sys.objects
where (type = 'U' or type = 'P')
and modify_date > dateadd(m, -3, getdate())
This approach will tell you what objects have changed, but not the specific changes.
Hi you can get the changed/modified db object details with this query
select name,create_date,modify_date
from sys.procedures
order by modify_date desc
Thanks
I don't think you're going to be able to find what you're looking for. SQL Server just doesn't track that information out of the box.
To handle this in the future, you can use some kind of source control (redgate, for one: http://www.red-gate.com/products/sql-development/sql-source-control),
or you can set up a DDL trigger (one such technique is described here: https://dba.stackexchange.com/questions/33541/how-to-keep-history-of-sql-server-stored-procedure-revisions/33544#33544).
I have a table I'm using as a source for an append query that calls upon a table query, which calls upon a union query to effectively adjust the eastern prevailing time to spring forward and fall back while converting to utc. there are only three fields in the table but I keep getting "access did not import .... due to type conversion". Please Help Me out!!! Thank you in advance
below is the access sql:
{append query}
INSERT INTO somePrice ( price )
SELECT DTQuery.Price
FROM DTQuery
WHERE (((DTQuery.EPT)<>[2ndsunday]));
{DTQuery}
SELECT
TransposeQuery.Field3 AS [Zone]
, DateSerial(Left([field1],4),Left(Right([field1],4),2),Right([field1],2))+[TransposeQuery]![Hour]/24 AS EPT, Val([Field8]) AS Price
, DateValue(DateSerial(Year([EPT]),3,14))-(Weekday(DateValue(DateSerial(Year([EPT]),3,14)),1)-1)+3/24 AS 2ndSunday
, DateValue(DateSerial(Year([EPT]),11,7))-(Weekday(DateValue(DateSerial(Year([EPT]),11,7)),1)-1)+3/24 AS 1stSunday
FROM TransposeQuery
ORDER BY
TransposeQuery.Field3
, DateSerial(Left([field1],4),Left(Right([field1],4),2),Right([field1],2))+[TransposeQuery]![Hour]/24, Val([Field8]);
First some general stuff: If you want to convert between timezones, which involves adding or subtracting a number of hours you might want to use to DateAdd function.
DateAdd("h", -2, [SourceDateTime])
This simply takes two hours off the SourceDateTime field.
Also rather than using the Left(Right( combination you can use Mid(string, start, length)
Mid("1234567890", 2, 4)
Returns 2345, but if you are dealing with dates just use DatePart
DatePart("h", "17/12/2011 08:10")
Returns 8.
As for the type conversion error, it's hard to say as you haven't given us the types of the fields in the destination table.
If you run the append query without the first line this will rule out the destination table. If it still fails then it might be in the where clause, so move the fields into the results set and make sure they are the same type. If it still fails then it must be in the source query so check DTQuery opens without any problems.