How to automatically update database SQL Server? - sql

I need a field (column) of my database to update automatically. For example, the field NUMBER for each record incrementing every minute. How can I do it? I am using SQL Server.

You're probably looking for something called SQL Server Agent jobs
here is a good starting point reference:
http://technet.microsoft.com/en-us/library/ms181153(v=sql.105).aspx
This allows you to run some sql code on a schedule of your choosing.
P.S.
If you have access to sql server management studio, the GUI is much nicer.

You can set up a SQL Agent job to run an update statement once a minute:
UPDATE tablename
SET NUMBER = NUMBER + 1

Job Agent is only for full SQL Server. Then best choise. If you using SQL express then can't. You can solve with update
UPDATE TABLE-NAME SET
VALUE = VALUE + 1

Related

How to capture truncate statement information in SQL Server 2012

I would like to capture the truncate statements information along with the user/Login information for all database in my production server.
Example:
Use mydb
go
truncate table deleteme_table
I would like to capture the information into the table like the below
Table Operation Database Login Time
deleteme_table Truncate mydb sandeep.pulikonda 17-12-2014 17:50:00
If the above scenario is not possible please suggest possible ways to capture it
I am using SQL Server 2012 Standard version. So granular level audit are not supported for that version.
you can use the SQL Server Audit functionality and add an audit for those queries.
this article explains in detail how to obtain this.
Another good way of profiling your SQL Server is using SQL Profiler. Here is a SO question similar to yours and an answer describing how to use SQL Profiler to achieve the results.
SQL Server Profiler - How to filter trace to only display TSQL containing a DELETE statement?

Creating EVENTS in SQL Server

I want to execute a query at a specific time. In MySQL we use events for that, example is as follows:
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
UPDATE myschema.mytable SET mycol = mycol + 1;
Please let me know how to do the same in SQL Server. Thanks.
PS : Sorry if its a repeated question. I tried searching for some time but didn't get it in SQL Server
You can use Sql server agent for this task and you can schedule there. Its easy way.
Another one is "waitfor time" check this link http://msdn.microsoft.com/en-IN/library/ms187331.aspx
(and you need to create a trigger with while loop logic and there wait for time is useful)
Another one create a bat file and schedule that in windows scheduler (if you are using sql sever express edition)

Update from linked server (mysql) to local sql database.

I am looking for a way to setup a scheduled update from a linked server I created to a local db, I am not familiar with triggers but from what I've read you have to set them up on the originating server, and I only have read access to the mysql Database. Basically all that I am trying to do is make a local copy of two tables from the mysql db. I can manually do so with select into statements, but I would like to have some automation if possible. Any thoughts on how to achieve this? Also I am using SQL server 2008 R2. Thanks!
You have several options to do:
Copy all data from the source table (do not use this if the source table is big)
If you have a column in the source table which can be used to determine which records should be copied, use that (this is mostly an auto updated timestamp column in MySQL)
Set up a trigger to track modifications
To copy, you can set up a Linked Server or you can use SSIS
To use a linked server you can use OPENQUERY()
You can schedule your task with SQL Server Agent

SQL Server 2005: problem with old SQL Server 2000 database

I attached database from SQL Server 2000 to SQL Server 2005 and it worked well but I had column called (Add Date) which had the Date time of input data and when I insert new data after attached data base to SQL Server 2005 the new data insert with same data 12.00 also it converted all old date to 12.00.
Please anyone help me how I can solved this problem also how can retrieved old Date time ?
I would start of with the obvious first
Check the columns properties to make there there isn't any default value being applied
Change the database compatablity level to make sure its set to SQL Server 2005 (90) (Right click on database Properties > Options Tab)
EDIT:
If you wanting to formated what date gets inserted into the (Add Date) field you can use the CONVERT or CAST function.
solution 1:
attach old database again, named it "old_bak"
and then write t-sql like this:
UPDATE XX
SET col = T.col
WHERE
XX.id = old_bak.dbo.XX.id
then the old data will retrieved!
solution 2:
rollback your database from .bak file
and execute your sql statement again
I doubt that your sql statement is "update", not "insert". or it won't be like that.

Delete column in SQL Server after a period from creation

I am working with sensitive/private files stored in SQL Server as VarBinary(MAX). Is there a way to tell the database or nHibernate to nullify the column after a period of time after its creation.
Make sure to put a timestamp column on the table, then set up a SQL Scheduled Job with a query to delete those rows periodically based on that time.
Absolutely not
Imagine the fun when SQL Server decides to change data apparently on it's own because the code monkey fluffed setting up whatever mechanism would be used...
You could (off top of my head):
Encrypt the columns
Schedule a clean up (SQL Agent job, a config table)
Don't use SQL Server for the files themselves, just store paths/links