Compare column names in excel sheet with column names in a table in SQL - sql

I have an excel sheet a large number of columns (~500) and a DB table with similar number of columns. I can run SQL queries on the database. What I want to achieve is to find the columns that are in my DB table but not in the spreadsheet. Checking it manually seems really inefficient.

Have you thought of using ODBC functionality within excel? You should be able to open up the table in excel then use count formulas to ensure that the numbers match.

You may try next semi-manual approach:
select top 1 * from [your table]
In SSMS right click to query results and select "Copy with headers"
Paste special->Transpose into Excel (pivot rows to columns)
Select headers in the existing excel, copy them, paste special-> transpose beside previous
Sort and compare columns

Related

Picking column names from an excel sheet in sql server while writing a macro

I am writing a macro for automating some task where i need to pick column names from an excel sheet , use that columns in select statement in sql and then fetch the values for those columns from database table. is there any way through which i can acheive this.
e.g.
in my DB there is a table employee having column names as - empname,empadd,empcontact ,empsal,dep,branch,location and 50 more columns.
my excel sheet is having 3 column names empname,empadd,laction.
now i want my code to pick only those 3 columns which are in excel and fetch the values from employee database.
i.e. select empname,empadd, location from employee;
i tried multiple things but no luck.
it would be great if anyone can suggest some way. thanks

Excel Pivot table updated missing columns

I have an Excel workbook which I use as template for a software written by me.
In some worksheets I create some tables (one per worksheet), which contains some tags, which will be replaced by my software with some data picked up from a SQL query and stored in a new Excel workbook. In the template file I create a pivot table based on two tables. But when the new file is created and the pivot updated, only the rows are updated with the new data coming from the query, while the columns are litterally missing. Why?
Table 1 in Template File Table 2 in Template File Table 1 with data from query Table 2 with data from query Pivot in Template File Pivot with data from query and missing columns
This looks like you are missing the column selections (PivotTable Fields) on Pivot Table. Make sure you have selected the required columns in the template.
Hope this helps.
Thank you. I resolved making a new query to link the two tables and then making pivot upon one table only. The problem was effectively having more tables in the same pivot table

How do I update an SQL table from an excel spreadsheet?

I have an excel spreadsheet with 2 columns and 1000 rows. I need to update a table an SQL with the data. I basically need it updates the column B values for the adjacent values from column A. How would I go about doing this?
You can use the SQL Server Import and Export Wizard. You can feed it comma delineated files and if the first row has the column names. It will do just about everything for you.

Replacing a value in an Excel column, based on its value in another sheet

I have a exported Excel sheet from SQL with records for insurance policies held by vendors that visit our office. There are two separate tables that identify the Insurer and the Vendor by a code. The table with the records of all the policies just has the codes for both of them.
Is there a formula that can look at another sheet for a value, and pull in another column from that sheet? Basically there is a key code for each vendor, and I need to replace that code with their actual name from another excel sheet.
Or if there is a simpler way to do this in SQL I could try that too.
In SQL you can JOIN the tables together:
SELECT i.col1,i.col2,v.col1
FROM Insurer i
JOIN Vendor v
ON i.key_code = v.key_code
Adjusting which columns you return as needed, without sample data it's unclear if there needs to be additional criteria in the JOIN.
In Excel you can use VLOOUKUP(), but I prefer INDEX() and MATCH(), below would try to match the value in B1 to a value on Sheet2 in B1:B20, and return the value from the corresponding row from Sheet2 A1:A20:
=INDEX(Sheet2!$A$1:$A$20,MATCH(B1,Sheet2!$B$1:$B$20,0))
There are numerous illustrated examples of either excel method that will be easy to follow.

Multiple Data Range in Pivot Table using VBA

I tried looking around for a solution but couldn't find one. Appreciate all the help on this.
Can you please tell me if there is a way to have data from two different sheets as the data source in a pivot table using VBA?
Example: There is a default data in Sheet 1, the user then selects "Sheet 3" from a dropdown, therefore the vba should create a pivot table (or refresh the current pivot table) with the data source as a combination of Sheet 1 and Sheet 3. Similarly, if the user then selects Sheet 5, Pivot table should be combination of Sheet 1 + Sheet 5. Since I plan to keep adding data to the excel file, this would be very useful.
Thanks!