Forgot to Drop Temp Table, now I can't get Rid of It - temp-tables

I wrote a stored procedure and created a temp table #TempHitRatioTable. I forgot to put the drop table statement in my procedure and now no matter what I do I can't get rid of the temporary table and can't re-execute the procedure. I have tried completely disconnecting from the server and reconnecting, but it is still there. I have tried the following statements:
IF OBJECT_ID('tempdb..#TempHitRatioTable') IS NOT NULL
DROP TABLE #TempHitRatioTable
and I also tried:
IF EXISTS(SELECT * FROM sys.tables WHERE name LIKE '#TempHitRatioTable%')
DROP TABLE #TempHitRatioTable
IF EXISTS(SELECT 1 FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'#TempHitRatioTable') AND type = (N'U')) DROP TABLE #TempHitRatioTable
But I still can't get rid of that table. My stored procedure is no longer running, nor is the crystal report I tried to run it on, but nothing seems to work. Please help.

I figured out what was happening. I had put the create table at the bottom of the cursor inside of it, so each time the cursor iterated throught to add a new record the table was already created. I moved the create table to the top of the stored procedure, before I created the cursor, added the statement: IF OBJECT_ID('tempdb..#TempHitRatioTable') IS NOT NULL DROP TABLE #TempHitRatioTable at the top of the procedure, then I added the drop table statement to the end of the procedure and all is well. I can run it as many times as I want and get all my results back not just one record. Silly me :-)

Related

Excel Data Queries - Ignore missing table / assign specific table number for every query

I am having a bit of trouble to create an automated report based on an HTML file. The file contains tables with data structured from the web page, and I just create tables from the tables recognized by Excel. So far it does what I need, but sometimes one or more tables from the HTML file is missing, and causing the tables to shuffle between them, like table 0 is missing then table 1 will take it's place and break the entire sheet because the wrong table is in the place of table 0.
What I wanted to know if it's a way to assign every query to a specific table number for each query. Like Table 0 will get the value from the specified query, not the first one that comes in the list of queries. The code so far is this for Power Query Editor:
let
Source = Web.Page(File.Contents("D:\AUTO.html")),
Data0 = Source{0}[Data]
in Data0
I use this code because the columns or rows will not always be the same, sometimes one can be missing and if I use the original code that is generated when getting the data from the page it will give errors and not load the table if there is a missing column/row.
Any help is appreciated.
MissingField.Ignore
When you use functions like Table.SelectColumns or RenameColumns or ReorderColumns you can use the MissingField.Ignore options to avoid the missing field error to stop your query
eg:
= Table.SelectColumns(#"blah",{"column1", "column2", "column3"}, MissingField.Ignore)
documentation:
https://learn.microsoft.com/en-us/powerquery-m/missingfield-error

Cannot delete "ghost" table from Power Pivot

I have created many power queries and power pivot tables in my Excel file. After some time, I think the data structure may have gotten corrupted, because this is happening:
In Data Model, I have this Community_Membership table that cannot be deleted. It is the only table that has this behavior (all other tables were deleted successfully)
After that, I went to Power Query to delete all existing queries AND CONNECTIONS, so that the list is empty like below.
But this table remains un-deletable there after I have deleted all queries from queries list.
Anybody know how to fix this?
I finally recreated the file as follows:
Make a new Excel file
Copy all of the sheets
Select all of the queries from the old file's query pane, hit copy
Go to the new file's queries pane, hit paste
Go into Query Editor and edit any names with conflicts or remove any redundant queries
The entire process takes about 10 minutes as I only had about 10 queries / sheets.
I suspect this happened because I created a query, added it to the data model then went back to Power Query and edit it (either its name or its content in some mysterious way). After that the link between PQ and PP is broken so that even after the original query is deleted, its clone in PP remains. Strange.
I encountered a similar situation after having renamed a query in Power Query : Power Pivot could see the new table under the new name, but the old table was still there in Power Pivot and could not be deleted.
Finally I came up with this solution :
Make sure there are no remaining relationships or measures in Power Pivot that refer to the table you want to delete.
Create a query in Power Query, having the same name as the table you want to delete. The content of the query does not matter. Make sure that the checkbox "load into the datamodel" is checked. As a result, the synchronization between Power Pivot and Power Query is restored.
Edit the query again and uncheck the checkbox. Now Power Pivot understands that we want to remove that table.
Now you can delete the query in Power Query.

Access TransferSpreadsheet Excel - Prevent Duplicates?

I am working on an application where there is a desire to automate data entry as much as possible. The wish is to add a button to such entry forms for choosing an excel file to import. I have done this for one interface, and now I'm working on others. I'm looking for the best way to prevent duplicates are imported into a table. For the one I am working on now, it is a simple 2 column import. One method I have used before is to import the spreadsheet into a temp table. Then I can utilize a query to insert where <> . I just wonder if this is the best method to use.
Any thoughts?
Thanks!
Something like this should work. I can tailor it more if you list some more details of your projects.
From "External Data" on the ribbon, link to the excel file.
Then write the following query:
INSERT INTO table1
(
field1,
field2
)
SELECT
a.field1,
a.field2
FROM tableExcel AS a
LEFT JOIN table1 AS b ON a.field1 = b.field1
WHERE (((a.field1) Is Null));
Then just attach a macro to the button running the query above.
I ended up finding the solution that will work best. I can put an index on the 2 fields that are getting imported from the spreadsheet, into the table. Then before I issue the transferspreadsheet command, I will set warnings false, and set them true once it is done. This way, the user won't get errors for the indexes doing their job of rejecting duplicates.
Anyone see any problem with that solution? The only bummer is that if I imported to a temp table, I could get a count of items first and verify the count after insert, so I could report some info to the user in the process. Other than that, this means I don't need a temp table, and I can go directly into the goal table without worry about importing dupes.

Sybase Dropping Temporary Table

Does anybody face an issue when you drop a temporary table at the Sybase ASE 12 it still persists at a current session. So you encounter "Table already exists" when trying to select data into it again
Well, you need to read the manuals, at least the syntax for the commands you expect to use, before you write code. Otherwise you will face issues at every turn. It depends on what you are trying to do.
SELECT ... INTO #MyTable creates a table and succeeds because it does not exist. So a second SELECT ... INTO #MyTable will try to create #MyTable, find that it exists, and fail.
If you want to perform a second SELECT into the same table, TRUNCATE the table, then use SELECT ... INTO EXISTING TABLE #MyTable.
Or DROP TABLE and skip the EXISTING TABLE modifier.
If you want the table to contain the sum of several SELECTS, obviously, skip the TRUNCATE.
I normally do this:
1) CREATE TABLE #temptable (
....
)
INSERT INTO #temptable
SELECT .....
This will never give error.
This solves another possible error . If the WHERE clause accompanying the "select INTO " yields no rows, the temporary table will not have zero rows but the temporary table won't be created at all. This could make the stored proc blow up later.

Adding columns to a sybase table with unique auto_identity index option

I've inherited a Sybase database that has the 'unique auto_identity index' option enabled on it. As part of an upgrade process I need to add a few extra columns to the tables in this database i.e.
alter table mytable add <newcol> float default -1 not null
When I try to do this I get the follow error:
Column names in each table must be unique, column name SYB_IDENTITY_COL in table #syb__altab....... is specifed more than once
Is it possible to add columns to a table with this property enabled?
Update 1:
I created the following test that replicates the problem:
use master
sp_dboption 'esmdb', 'unique auto_identity indexoption',true
use esmdb
create table test_unique_ids (test_col char)
alter table test_unique_ids add new_col float default -1 not null
The alter table command here produces the error. (Have tried this on ASE 15/Solaris and 15.5/Windows)
Update 2:
This is a bug in the Sybase dbisql interface, which the client tools Sybase Central and Interactive SQL use to access the database and it only appears to affect tables with the 'unique auto_identity index' option enabled.
To work around the problem use a different SQL client (via JDBC for example) to connect to the database or use isql on the command line.
Should be no problem to ALTER TABLE with such columns; the err msg indicates the problem regards something else. I need to see the CREATE TABLE DDL.
Even if we can't ALTER TABLE, which we will try first, there are several work-arounds.
Responses
Hah! Internal Sybase error. Open a TechSupport case.
Workaround:
Make sure you get jthe the exact DDL. sp_help . Note the IDENTITY columns and indices.
Create a staging table, exactly the same. Use the DDL from (1). Exclude the Indices.
INSERT new_table SELECT old_table. If the table is large, break it into batches of 1000 rows per batch.
Now create the Indices.
If the table is very large, AND time is an issue, then use bcp. You need to research that first, I am happy to answer questions afterwards.
When I ran your sample code I first get the error:
The 'select into' database option is not enabled for database 'mydb'. ALTER TABLE with data copy cannot be done. Set the 'select into' database option and re-run
This is no doubt because the data within your table needs copying out because the new column is not null. This will use tempdb I think, and the error message you've posted refers to a temp table. Is it possible that this dboption has been accidentally enabled for the tempdb?
It's a bit of a shot in the dark, as I only have 12.5 to test on here, and it works for me. Or it could be a bug.

Resources