SQL-Server generate data row once per day - sql-server

I'm quite a newbie to the topic databases. It may be that the topic
already exists, but I don't even know how to search for it.
How to query and so on seems to be no problem.
And how to write a trigger on an INSERT/DELETE/UPDATE statement is known too.
I would like to know if it is possible to execute some code to generate a data row
every day once at a specific time without an external programm.
Thanks, Chris
PS: I'm from Germany, so don't get angry about my expressions, please^^

If you are not in an Express Edition you may create an SQL Job to do that.
Open SSMS
Drill Down your server instance
Drill Down SQL Server Agent
Right click Jobs folder
Chose New Job
Fill in the details in the General Tab
Go to the Steps tab
Click New
Enter a name, choose which db to run against and enter your SQL statement.
Go to Schedules tab
Add a schedule to run once each day at the time you wish.

Related

Script that will make a SQL Server script from a database

I have a SQL Server database that contains tables and other objects that I would like to script out on a regular basis. The idea is that I am going to create an autobuild that will create a container that is a scaled down version of this database (for testing).
I could easily just go script the database manually, but then I have to keep the resulting script up-to-date.
I am wondering if there a way to programmatically connect to a running SQL Server instance and (based off some input) generate scripts of specific tables, stored procedures, user defined types and data?
If you're coding with .NET other other languages that can call .NET objects, you can just use the scripting classes in SMO (SQL Management Objects).
There are worked examples in the SQL Server documentation here:
https://learn.microsoft.com/en-us/sql/relational-databases/server-management-objects-smo/tasks/scripting?view=sql-server-ver15
They are designed to do exactly what you're requesting.
Well, I would think a backup would be the easiest way to go.
Or, setup several jobs, wrap everything in a SProc, and schedule that to run on whatever frequency you desire.
To create and attach a schedule to a job
In Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance.
Expand SQL Server Agent, expand Jobs, right-click the job you want to schedule, and click Properties.
Select the Schedules page, and then click New.
In the Name box, type a name for the new schedule.
Clear the Enabled check box if you do not want the schedule to take effect immediately following its creation.
For Schedule Type, select one of the following:
Click Start automatically when SQL Server Agent starts to start the job when the SQL Server Agent service is started.
Click Start whenever the CPUs become idle to start the job when the CPUs reach an idle condition.
Click Recurring if you want a schedule to run repeatedly. To set the recurring schedule, complete the Frequency, Daily Frequency, and Duration groups on the dialog.
Click One time if you want the schedule to run only once. To set the One time schedule, complete the One-time occurrence group on the dialog.

How to import tables of a database in SQL Server to another SQL Server on a different computer programmatically?

Good day :-), I'm a beginner when it comes to asking questions here so please bear with me..
First, let me state the example problem at hand:
Company "A" has 4 branches, each placed in different provinces. Each branch, including the main company has an application that inputs a person's profile and saves it to SQL Server database. All data entered by the branching companies must be submitted to the main branch once in a month. But the main director doesn't want those data to be submitted via cloud, he wants it in a flash drive and one individual from each of those branches must travel to the main company, and submit the flash drive with the data inside.
Because if I just use backup & restore method, they will overwrite everything.
Thus, I have created a dummy database to hold those data. Same table, and each table column has the same data type as the original.
Using VB.NET how does one export the table of a database to a dummy database programmatically?
And once exported, how does one import it to the main company's database?
I'll be grateful if you provide me solutions or if you can point me out to the right direction. Also is my method correct?
You can import/export data from one server to another by using Linked Server.
we can do this also by using sql scripts for using simple select & insert query referring the "Servername.Database.tableName"
Now, if you need to do this in a timely manner, for example for every night you need to import/export the data, what i would suggest is to use Sql Agent. Here you can provide timing to execute the script such as date time and whether it is recursive etc...
with the script of import /export to targeted to specific server.
Ps:
Other approch would be to you use SSIS.
Hope this helps.
Thanks,
Satish

ReportServer Execution Log look before 2 months

is there a way that I can look into the report server execution log file before two months? I would like to dispose a reportserver database and want to see which report is last run. Sql Server Execution log file only stores for 2 months,but I want to see the log before that.I am using the Microsoft Sql Server 2005.
Thank you very much.
Short answer is no... As you've seen this is 60 days by default; after that SSRS will delete any old entries from its internal ExecutionLog table.
What you can do is change this default:
Server Properties (Logging Page)
This is not much use for your existing data but at least this might let you collect the information you need from now on.

Want to create a script to export Data and tables and views to a sql script

Want to create a script to export Data and tables and views to a sql script.
I have SQL Server 2008 r2.
So far I've only been able to automatically generate an sqlscript for all tables and views. But the data wasn't included.
OR is there any easier way to export data, tables, views, from one SQL Server to my ISP's SQL server?
Regards
Tea
If for some reason a backup/restore won't work for you, SSMS' Generate Scripts tool includes an advanced scripting option to include data:
Here are some options to think over (prioritised in terms of what I would recommend):-
A simple backup and restore will be the easiest and quickest solution;
Using a data scripting tool (like Red-Gate's Data Compare) could solve your needs;
Use the database comparison as part of Visual Studio.
A SSIS package could be developed to pump data back and forth between the two instances; or
Write your own script using the SET IDENTITY INSERT ON / OFF command for the identity seeded tables
The easiest way to do this is to create a backup, copy the .bak file to the other server, and restore the backup there.
Like #jhewlett said that will be the best way to do it. to answer the question in the comment section. no it shouldn't be a problem. Just make sure that the SQL Server Versions are the same. Had a bit of an issue not to long ago where there were two pc's with different releases of the R2 installed and couldn't restore the backup. Other thing you can also do is to script the entire database with data, but this will not be recommended as it could take a long time to generate the script and for it to finish running on the other computer.
Or you can simply just stop the SQL server instance and copy the database away onto an external hard drive and re-attach it to the other server. just remember to start the instances after doing this step.
I use Navicat Premium for these kind of things in mysql. It generates sql from data, tables, views and anything else. It provides tools to copy or synchronize table from one database on different server or platforms as well. For example I use it so much to transfer my tables from MySQL to a SQLite database, So easy and fast. Otherwise I had to transfer it manually with so much trouble.
very good tool and required for any DB admin or programmer. It support MySQL, Oracle, MS SQL Server, PostgreSQL and SQLite.
To Generate a schema with data follow these steps.
Select database to generate a schema '>' right click '>' Tasks '>' Generate schema '>' click NEXT in popup window '>'
select DB object to generate schema and click NEXT '>'
Go to advance option and scroll down '>'
Find Type of data to script and select one option as you need. '>'
and then Next Next and finish it.
Enjoy it.
If you don't want to port all tables data (for example you need to port just some base data in special tables) scripting options is not useful for you. In this case you'll have two options. First is using some third parties tools such as Red-Gate and Second way is writing the script by yourself. I prefer Second option because except the expensive price of most of them i want to run just little script for little delete, update and inserting purpose. But the important problem is here: maybe the record count is too long to write scripts record by record. I Think the linked Server is good point to solve that. It's enough for describing Just Declare Linked Server as you see in Images and get new script in your source DB and write scripts with access to both source and destination DB. Attached image must be clear.
Create New Linked Server:
Write Destination SQL Server Address:
Fill Login Info:
Now you have Linked Server:
Write script and enjoy:
Hope this help.

How do I log every data change that has been made in SQL Server?

I have a profile page (less than 10k users) and I need to track every change and update that is made through out the application, by the users. Whether a user updates the profile picture or adds an extra space in a "comments" field, I need to store the previous data.
In short: I need to store everything.
Is there some sort of "tracking-history-and-changes-in-sql-server" software or do I have to implement it myself?
If I have to implement it myself I can think of three ways to do it:
I keep everything in the same table
but I add a key column that
specifies which row is active
and which is old.
I add a new table called history
where I store the column name that had the
change, when it was changed and what
the old data was.
I add a history table for each table
in the database. It looks the same
but only keep track of each tables
history.
Has anyone had a similar problem and how did you solve it?
This was built using mvc 4 and it's a
normal website.
EDIT
I'm mostly interested in existing solutions/software, but If there are none I would have to do it myself.
Has anyone used SQL Data Compare?
Where I worked last everything had to be logged fully. (working with goverment organisations). We never updated or deleted data.
What you would do is have a start date and an end date on each row. To do an update you would update the old data to have an enddate then insert a new row in the table. To do a delete you would put an enddate on the row with a null enddate. We also had an "updated by" column to put the userid
I used the third approach to do that but didn't create a history table for all tables of my DB but history tables for most important tables. You can use triggers to do that , create trigger for Update. You can read more about Triggers here and here
Microsoft offers Change Tracking and Change Data Capture for awhile now. These technically offer the tracking of all your changes in your database, which suits your purpose. Just note that CT is available in most versions, whereas CDC used to be only available in Enterprise until SQL Server 2016, where they made it available for Standard too.
ApexSQL Log does pretty much the same as well, but if you're using SQL Server then it's integrated in your software already.
have you considered enabling full transaction logging on your database and then using some of the log reading tools to monitor data changes. ApexSQL Log is by far the best log reader on the market but there are other solutions out there. SQL Log Rescue from Red Gate is free but it's only for sql server 2000.
Using this approach you dont need to make any other changes in your database or in your application since every transaction is automatically logged when database is in full recovery mode.
Hope this helps.

Resources