Db2 select certain columns not working after set Integrity - database

Im learning DB2 and I had a problem while testing some options in my db.
I have 2 tables like this:
Country
=========
IdCountry -- PK
Name
State
=========
IdState -- PK
IdCountry -- FK to Country.IdCountry
Name
Code
And I am using queries like:
SELECT IdState, Name
FROM Tables.State
WHERE IdCountry = ?
Where ? is any working IdCountry, and everything worked fine.
Then I used set integrity in my db2 control center using the default info in the options and the process was successful but now my query isn't giving me any results.
I tried using :
SELECT *
FROM Tables.State
Where IdCountry = ?
and it gives me back results.
While making tests to the table I try adding new States and they appear in the query using column names instead of *, but old records still missing.
I have no clue about what's happening, does anyone have an idea?.
Thanks in advance, and sorry for my poor English.

I'm assuming here that you're on Linux/Unix/Windows DB2, since z/OS doesn't have a SET INTEGRITY command, and I couldn't find anything about it with a quick search on the iSeries Info Center.
It's possible that your table is still in "set integrity pending" state (previously known as CHECK PENDING). You could test this theory by checking SYSCAT.TABLES using this query:
SELECT TRIM(TABSCHEMA) || '.' || TRIM(TABNAME) AS tbl,
CASE STATUS
WHEN 'C' THEN 'Integrity Check Pending'
WHEN 'N' THEN 'Normal'
WHEN 'X' THEN 'Inoperative'
END As TblStatus
FROM SYSCAT.TABLES
WHERE TABSCHEMA NOT LIKE 'SYS%'
If your table shows up, you will need to use the SET INTEGRITY command to bring your table into checked status:
SET INTEGRITY FOR Tables.State IMMEDIATE CHECKED

Related

How to filter "show tables"

I would like to filter the output of show tables.
The documentation has one example on how to do this using result_scan(last_query_id()), but for me the example does not work:
show tables;
select "schema_name", "name" as "table_name", "rows"
from table(result_scan(last_query_id()))
where "rows" = 0;
-- SQL compilation error: error line 1 at position 8 invalid identifier 'SCHEMA_NAME'
The column SCHEMA_NAME is actually in the output of show tables,
so I do not understand what is wrong.
Best,
Davide
Run the following on your account and see what it is set to:
show parameters like 'QUOTED_IDENTIFIERS_IGNORE_CASE';
If this is set to TRUE, then it is ignoring the quotes in your query, which will then uppercase the column names, which won't match to the lowercase names of the SHOW output.
To resolve for your own session, you can run the following:
ALTER SESSION SET QUOTED_IDENTIFIERS_IGNORE_CASE = False;
You can also change this at a user or account level, if you wish. Setting this value to TRUE isn't recommended for the reason that you are running into.
You can reference the filter column using $<col_n> syntax (#8 for rows).
Example:
show tables;
select *
from table(result_scan())
where $8 > 5
That being said, your query worked for me.

Not able to get the load history from INFORMATION_SCHEMA.COPY_HISTORY

I have a Snowflake table which gets its data (via COPY INTO) from an S3 bucket. When I tried to run the below statement to check the load status, it didn't give any result.
SELECT * FROM TABLE(INFORMATION_SCHEMA.COPY_HISTORY(TABLE_NAME=>'HourlyTransactionStaging', START_TIME=> DATEADD(DAY, -14, CURRENT_TIMESTAMP())));
Instead, I got this error
Table DBNAME.STAGING.HOURLYTRANSACTIONSTAGING did not exist or was purged.
However, when I tried to run this, it ran and gave me the results as well.
select * from information_schema.load_history
Where
Schema_name = 'STAGING'
AND TABLE_NAME = 'HOURLYTRANSACTIONSTAGING';
I figured out what the issue was. Apparently, TABLE_NAME parameter in the COPY_HISTORY function is case sensitive and I was providing the table name as per the conventions.
HourlyTransactionStaging --> HOURLYTRANSACTIONSTAGING
Glad you figured it out. Also you need to make sure that you're on the correct database / schema before running the query as below:
use schema your_db.schema;
select *
from table(information_schema.copy_history(table_name=>'table_name', start_time=> dateadd(hours, -1, current_timestamp())));

Moving Access update query to SQL server

We are migrating a Access database to Azure, one update queries is not working despite trying several syntax changes and removals of spaces.
Below is the design view of the query in Access:
Query Design
The following is the SQL expression of the update query in Access:
UPDATE SPEND_steve, KeywordRULES
SET SPEND_steve.Category = [KeywordRULES].Category
WHERE (((SPEND_steve.Category) Is Null) AND ((SPEND_steve.ItemDescription) Like "*"
And (SPEND_steve.ItemDescription)=[KeywordRULES].[ItemDescription]
And (SPEND_steve.ItemDescription) Like "*"));
With the above I receive error 102: Incorrect syntax near ','.
Thank you in advance for any help to move this functioning query from Access to SQL server!!!
Try removing ", KeywordRULES" from Line 1, an UPDATE statement is only able to update one table (or view) at a time.
UPDATE SPEND_steve
SET SPEND_steve.Category =
[KeywordRULES].Category WHERE (((SPEND_steve.Category) Is Null) AND
((SPEND_steve.ItemDescription) Like "" And (SPEND_steve.ItemDescription)=
[KeywordRULES].[ItemDescription] And (SPEND_steve.ItemDescription) Like "")) ;
SQL Update syntax is a bit different to Access, in that each update statement should affect a single table. You can however reference other tables through a join or other ways depending on what you are trying to do.
So KeywordRules is implicitly joined in your query. So your intention is to update the SPEND_steve table based on info from the KeywordRules table. You can do this by a join to the keyword rules.
Update SPEND_steve
Set SPEND_steve.Category = [KeywordRULES].Category WHERE
(((SPEND_steve.Category) Is Null) AND ((SPEND_steve.ItemDescription) Like ""
And (SPEND_steve.ItemDescription) Like ""))
JOIN KeywordRules on (SPEND_steve.ItemDescription)=[KeywordRULES].[ItemDescription];
Also you should change like "" to = "" which may yeild a performance increase, although you might want to check for AND IS Not NULL as well if it could be null.

Update select statement from subselect statement result array

I have a query that I have been working on for a little bit, and I can kind of understand why it's in error, however I cannot seem to figure out how to make everything function.
My setup looks like this.
I have a database table that contains the a list of sites
table name: websites
column name within websites:
domain_name
------------------
site1234.mydomain.com
site1235.mydomain.com
site1256.mydomain.com
site1257.mydomain.com
site1258.mydomain.com
What I am looking to do is to change everything after the subdomain section so ie: .mydomain.com -> .mynewdomain.com
I have the query to get the first subdomains just fine.
SELECT split_part(domain_name, '.', 1) AS site_name FROM websites;
So I understand that this needs to be in a subselect, but I am not sure what I am missing. My select/subselect query looks like this.
UPDATE websites SET domain_name = site_name || '.mynewdomain.com' FROM (SELECT split_part(domain_name, '.', 1) FROM websites) AS site_name WHERE domain_name LIKE '%.mydomain.com';
I understand why it is in error because my first query returns more than one row, so it sees the data mismatch, I know I need to some how loop through each result in the subselect and use that value in the initial select, but I don't know what syntax I would need to make this function.
Any help is greatly appreciated!
Cheers,
-H
UPDATE websites
SET domain_name = t.site_name || '.mynewdomain.com' FROM
(
SELECT split_part(domain_name, '.', 1) AS site_name
FROM websites
) t
WHERE domain_name LIKE '%.mydomain.com';

How to check if SQL Server Tables are System Tables

Using the stored procedure sp_msforeachtable it's possible to execute a script for all tables in a database.
However, there are system tables which I'd like to exclude from that. Instinctively, I would check the properties IsSystemTable or IsMSShipped. These don't work like I expect - I have for example a table called __RefactorLog:
But when I query if this is a system or MS Shipped table, SQL Server reports none of my tables are system tables:
exec (N'EXEC Database..sp_msforeachtable "PRINT ''? = '' + CAST(ObjectProperty(Object_ID(''?''), ''IsSystemTable'') AS VARCHAR(MAX))"') AS LOGIN = 'MyETLUser'
-- Results of IsSystemTable:
[dbo].[__RefactorLog] = 0
[schema].[myUserTable] = 0
and
exec (N'EXEC Database..sp_msforeachtable "PRINT ''? = '' + CAST(ObjectProperty(Object_ID(''?''), ''IsMSShipped'') AS VARCHAR(MAX))"') AS LOGIN = 'MyETLUser'
-- Results of IsMSShipped:
[dbo].[__RefactorLog] = 0
[schema].[myUserTable] = 0
When I look into the properties of the table (inside SSMS), the table is marked as a system object. An object property like IsSystemObject doesn't exist though (AFAIK).
How do I check if a table is a system object, apart from the object property? How does SSMS check if a table is a system object?
Management studio 2008 seems to run some quite ugly following code when opening the "System Tables" folder in the object explorer, the key bit seems to be:
CAST(
case
when tbl.is_ms_shipped = 1 then 1
when (
select
major_id
from
sys.extended_properties
where
major_id = tbl.object_id and
minor_id = 0 and
class = 1 and
name = N''microsoft_database_tools_support'')
is not null then 1
else 0
end
AS bit) AS [IsSystemObject]
(Where tbl is an alias for sys.tables)
So it seems that it's a combination - either is_ms_shipped from sys.tables being 1, or having a particular extended property set.
__refactorlog is, in contrast to what SSMS suggests, a user table. It is used during deployment to track schema changes that cannot be deduced from the current database state, for example renaming a table.
If all your other user tables are in a custom (non-dbo) schema, you can use a combination of the isMSshipped/isSystemTable attributes and the schema name to decide if a table is 'in scope' for your script.
In the past I've worked on the assumption that, in the sys.objects table, column is_ms_shipped indicates whether an object is or is not a system object. (This column gets inherited by other system tables, such as sys.tables.)
This flag can be set by procedure sp_ms_markSystemObject. This, however, is an undocumented procedure, is not supported by Microsoft, I don't think we're supposed to know about it, so I didn't tell you about it.
Am I missing something?
However, there are system tables which I'd like to exclude from that
At least on SQL Server 2008, sp_MSforeachtable already excludes system tables, as this excerpt from it shows:
+ N' where OBJECTPROPERTY(o.id, N''IsUserTable'') = 1 ' + N' and o.category & ' + #mscat + N' = 0 '

Resources