Showing posts with label SQLServer. Show all posts
Showing posts with label SQLServer. Show all posts

Friday, July 24, 2020

SQL Query to find all tables in a Database containing specified column name

Following query will help to find field name available in any of the table in a database.

SELECT * from INFORMATION_SCHEMA.COLUMNS 
where COLUMN_NAME like '%ColumnName%' 
order by TABLE_NAME



SQL Server:

SELECT Table_Name, Column_Name 
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_CATALOG = 'YOUR_DATABASE'
AND COLUMN_NAME LIKE '%YOUR_COLUMN%'


Oracle:

SELECT owner, table_name, column_name 
FROM all_tab_columns 
WHERE column_name LIKE '%YOUR_COLUMN_NAME%'

AND OWNER IN ('YOUR_SCHEMA_NAME');

Thursday, December 5, 2019

Get Table Name with Row Count from SQL Database

Select the database you want to filter the information about the row count and run following SQL Statement

SELECT 
    T.NAME AS 'TABLE NAME',
    P.[ROWS] AS 'ROW COUNT'
FROM SYS.TABLES T 
INNER JOIN  SYS.PARTITIONS P ON T.OBJECT_ID=P.OBJECT_ID

This help me to know how many rows are there in table. Hope this will help you too.

Saturday, November 18, 2017

Conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value

Solution:
"The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value" with Entity Framework when calling SaveChanges.

Go into your EDMX file, select the field that is causing the error and set StoreGeneratedPattern to Computed.








Add the DatabaseGenerated attribute if you are using EF Code First:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

public Nullable<System.DateTime> Created { get; set; }

Wednesday, April 6, 2016

How to Test a Database Connection String using NotePad

Create and Configure a Universal Data Link (.udl) File with Notepad.

I just came across a way to test a data provider’s connection string (like a SQL Server database) with the help of a plain text file using Notepad.  To investigate and test out if your connection string works, you’re going to want to create a UDL file. 

To do this, follow these steps:
1.Open up Notepad and create an empty text file, then click File -> click Save -> and save it with the File name: TestConnection.udl to your desktop.
2.Go to your desktop and double-click on the TestConnection.udl file you just created and the Data Link Properties box will popup.
3.Select the Provider tab and Find the provider that you want to connect with and click Next >>
4.Now from the Connection tab, select or enter your source/ server name  then enter information to log on to server -> and select the database on the server. 
5.Click Test Connection and click OK to save the file.

Note: If errors occur during testing of your connection string, you will get a popup box with the error message.

Once, you've successfully tested your connection string, now go and compare the details of your TestConnection.udl with your (website) project connection string to see if they are similar.



Monday, August 26, 2013

SQL Database Backup with Query

The tail of the transaction log usually refers to the contents of the database's transaction log that has not been backed up.  Basically, every time you perform a transaction log backup, you are backing up the tail of the transaction log.

Then why all the fuss over this?  Well, the complication starts when the database's data files are no longer available, perhaps due to a media failure.  When this occurs, the next logical step is to back up the current transaction log, and apply this backup to the standby database.  You can back up the transaction log even though the data files are no longer available, using the NO_TRUNCATE option e.g.

BACKUP LOG AdventureWorks TO DISK = 'G:\Backups\AdventureWorks_log_tail.bak' WITH NO_TRUNCATE

You can then use the resulting log backup to bring the standby database to the state the database was in before the failure.

This is another good reason to place your transaction log files on different disks from the data files.  If they were on the same disks, a disk failure would prevent you from taking a backup of the transaction log.

Another complication is when your database is using the bulk-logged recovery model, and the current transaction log contains minimally logged transactions.  In this situation, a transaction log backup needs to store the modified data pages (extents).  If the data files are not available, you cannot back up the transaction log, even with the NO_TRUNCATE option.

Lastly, in SQL Server 2005 and above, every time you try to restore a database which already exists, is using the full or bulk-logged recovery models, and the transaction log contains active transactions, an error similar to the following is displayed:

Server: Msg 3159, Level 16, State 1, Line 1

The tail of the log for the database "AdventureWorks" has not been backed up. Use BACKUP LOG WITH NORECOVERY to backup the log if it contains work you do not want to lose. Use the WITH REPLACE or WITH STOPAT clause of the RESTORE statement to just overwrite the contents of the log.
Server: Msg 3013, Level 16, State 1, Line 1

RESTORE DATABASE is terminating abnormally.

This is just SQL Server's way of telling you that there are log records in the transaction log that have not been backed up. If the current transaction log can be discarded, you can use the REPLACE option to tell SQL Server to ignore the current transaction log e.g.

RESTORE DATABASE AdventureWorks FROM DISK = 'G:\Backups\AdventureWorks_full.bak' WITH REPLACE

Reference Page : http://www.sqlbackuprestore.com/backingupthetail.htm
Reference Site: SQLBackupRestore.com