Rows counts are not matching after migration from Oracle to SQL Server - sql

I migrated database from oracle to Sql server using sqlmigration(SSMA) tool.
After migration, report say 100% successfully migrated but the row counts are more than oracle in Sql Server. I cant figure out why its showing more row counts than oracle. Please Help

You can check row counts of all the tables in sql server using below query
SELECT
t.name, i.rows
FROM sys.tables t
INNER JOIN sysindexes i
ON t.object_id = i.id
WHERE i.indid < 2
Similarly for oracle you can use this query
Get counts of all tables in a schema
and you can compare

perform row count like ->
-- Source
select count(1) from table_source;
-- Target
select count(1) from table_target;

Related

Sql query to get total records for specific table available in multiple databases on same server

I have one requirement where I need to find the total number of records in specific table "ENTITY", available in all the databases that we have on our SQL Server. I want to write a single query to get records from multiple databases for this table. Can someone please help me in this regard?
Thanks.
Option One
SELECT SUM(P.[Rows]) AS [Count]
FROM SYS.objects O
JOIN SYS.partitions P
ON P.[object_id] = O.[object_id]
AND P.index_id < 2
WHERE O.name = 'Table'
Option Two
SELECT COUNT(1)
FROM dbo.Table

Get column differences between 2 databases (SQL Server)

I've got 2 databases almost identical to one another. However, it seems that for some tables in the new database, they are missing columns that are in the old database.
What would be the best way to see the differences between columns in tables between 2 databases? Specifically, I need to see what columns AREN'T in the new database that ARE in the old one.
I've tried looking this up but most things I found were either not what I needed or looking at "records".
You can query the columns from your db using the sys tables and compare the result sets. This script assumes your old db has all the columns you want.
;WITH old_db_columns AS (
SELECT c.object_id, c.column_id, c.name AS column_name, t.name AS table_name
FROM old_db.sys.tables t
INNER JOIN old_db.sys.columns c
ON t.object_id = c.object_id
)
, new_db_columns AS (
SELECT c.object_id, c.column_id, c.name AS column_name, t.name AS table_name
FROM new_db.sys.tables t
INNER JOIN new_db.sys.columns c
ON t.object_id = c.object_id
)
SELECT *
FROM old_db_columns o
WHERE NOT EXISTS (
SELECT 1
FROM new_db_columns n
WHERE n.table_name = o.table_name
AND n.column_name= o.column_name)
You may use SQL Compare and SQL Data Compare, tools by Red Gate, to compare and sync databases schema and data.
You can generate the create statement of the tables and you can compare them with using any diff tool.
Check out that video: VS Comparision
Visual Studio has built in functionality that you are able to do data compares, schema compares and it will generate the differences for you in a script if you need to recitfy the variances.

Query in SQL Server 2000

I have 2 SQL Server databases with same structure, first one in SQL Server 2008 and the other in SQL Server 2000.
I wrote a query in SQL Server 2008 like this :
SELECT
sph.SiProforma, SuProforma, cps.Tp_FamilyOffice_Name,
DsProforma, NaProformaFee, NqCount,
NaProformaFee * NqCount as jameradif,
SUM(NaProformaFee * NqCount) OVER (PARTITION BY suProforma) AS ghabelepardakht
and it works in SQL Server 2008, but when I run it in SQL Server 2000 I get this error :
Incorrect syntax near the keyword 'OVER'.
How can I fix it? Or replace code ?
SQL Server 2000 doesn't support OVER. Write a sub query that performs a SUM(..) GROUP BY suProforma and join the sub query to the main table on suProforma
I'd have written an example for you using your tables but for 2 reasons: one that, I'm posting from an iPhone 4 and reformatting your query (it's a bit of a mess- you will do yourself more favours keeping your code neater) is very very hard work and 2, it's not obvious where suProforma column is kept in which table
Here is a simpler example you can apply to your query:
SELECT
t1.*,
sq1.summed
FROM
table1 t1
INNER JOIN
(SELECT sum(a*b) as summed FROM table1 GROUP BY suProforma) sq2 ON T1.suProforma = sq2.suProforma
To apply this query to your situation replace table1 with the table that has suProforma and correct the SUM(a*b) with your actual columns. Then add your other joins in
If he columns you are grouping by and summing are in different tables, then you will have to write a query that links the two together and paste that in (surrounded by brackets) in place of table1
For example these two things are the same:
SELECT *
FROM table1
SELECT *
FROM (select * from table1) sq1
Get into the habit of looking at any entity in the FROM part of a query as "just a rectangular block of data from which I can select results"
This query will work in both SQL Server 2000 and 2008

How to get column names from a query in SQL Server

Using SQL Server.
I have a very extensive query, with a lot of aliasing, etc...
Is there a way, using just SQL (stored proc is fine, but not PHP, etc), to get a list of all column names from this query? (I realize I will have to probably embed my query inside of this solution but that is fine. Just a temporary measure.)
Thanks!
If you're using SQL Server 2012 or later you can take advantage of sys.dm_exec_describe_first_result_set
SELECT name
FROM
sys.dm_exec_describe_first_result_set
('Your Query Here', NULL, 0) ;
DEMO
There are various ways that you can get the columns out of the query, such as:
select top 0 s.*
from (<your query here>) s;
Then you can parse the results.
However, I have found another approach useful. Create either a view or a table using the same logic:
select top 0 s.*
into _TempTableForColumns
from (<your query here>) s;
Then use information_schema (or the system tables if you prefer):
select *
from information_schema.columns
where table_name = '_TempTableForColumns' and schema_name = 'dbo';
drop table _TempTableForColumns;
The advantage of this approach is that you can get type information along with the column names. But the engine still has to run the query and that might take time even though no rows are returned. Although the column names and types are available after compiling, I am not aware of a way to get them without also executing the query.
After SQL Server 2008
select *
from sys.columns c
inner join sys.objects o on c.object_id = o.object_id
where o.name = 'TableName'
Before
select *
from syscolumns c
inner join sysobjects o on c.id = o.id
where o.name = 'TableName'

How to use inner join in oracle 11g?

I am running my Oracle query here but it's not working but the same query is working in SQL Server
Here is my query:
SELECT d.dept_code,
d.dept_name,
d.dept_desc,
e.comp_name
FROM dept_master d
inner join comp_master e
ON d.comp_id = e.comp_id
where in dept_master.comp_id value is the same as in Dept_Master table.
The reason you are not getting any result is mainly because of the data
do this check to see if data is available in the tables
select * from dept_master;
select * from comp_master;
and see if both tables have any matching rows, i.e.; at least 1 row has same comp_id in both tables
I hope you will find the answer after doing this exercise
Is comp_id a character field? In that case define it as VARCHAR2 in Oracle. or try trim(d.comp_id) = trim(e.comp_id)
See a demonstration in SQL Fiddle.