TABLE(RESULT_SCAN()) is behaving differently for Clustered Inserts - snowflake-cloud-data-platform

I am trying to fetch record counts from table(result_scan()) for clustered tables.
Table - 1 : clustered by 1 key
select sum("number of rows inserted") from TABLE(RESULT_SCAN(query_id)). This worked and gave back record count.
Table -2 : clustered by 2 keys.
Could not execute the record count query:
select sum("number of rows inserted") from TABLE(RESULT_SCAN(query_id)). Error Message obtained is 000904 (42000): 01929f09-01a8-3a2c-0000-54ad005bcfc2: SQL compilation error: error line 1 at position 11
invalid identifier '"number of rows inserted"'
Not sure why the same query works in one scenario and throws invalid identifier in another?

As I see, the COPY command does not return the "number of rows inserted" column. If you want to get the number of rows inserted by COPY command, you should sum the rows_loaded column:
select sum("rows_loaded") from TABLE(RESULT_SCAN());
Please check: https://docs.snowflake.net/manuals/sql-reference/sql/copy-into-table.html#output

Related

SQL Server changes the value in the float column when converting to varchar

I have a column in my table that is of float type. The table was automatically generated when I imported the spreadsheet (Excel) data to my database. Thus there is a column I wish to change from float to varchar, but when I try to do this, I get an error:
'tblInvoices' table
Unable to create index 'IX_tblInvoices'.
The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.tblInvoices' and the index name 'IX_tblInvoices'.
The duplicate key value is (1.00001e+006). The statement has been terminated.
It is a unique column, and set that way (not set as the primary key for reasons). I have already run queries to search for and delete duplicate fields but there are none. The query I ran as follows:
WITH CTE AS
(
SELECT
Invoice,
RN = ROW_NUMBER()OVER(PARTITION BY Invoice ORDER BY Invoice)
FROM
dbo.tblInvoices
)
DELETE FROM CTE
WHERE RN > 1
So the value within the Invoice column is 1000010 and when I run the following query a single row is found.
SELECT *
FROM [TradeReceivables_APR_IFRS9].[dbo].[tblInvoices]
WHERE Invoice = 1.00001e+006
Note that I have searched for the value in the error, 1.00001e+006, and not 1000010.
So my question is why does the DBMS do this? Why does it change the value like that? When I remove the column, it does it with another column and so on and so on (about 40 000 rows in total). How can I change the column from float to varchar without changing the data and getting errors?
Any help will be greatly appreciated!
It seems that the field is an integer so you can Cast it to BIGINT before cast to VARCHAR
Declare #Invoice as float = 1.00001e+006
print cast(#Invoice as varchar) -->> Result : 1.00001e+006
print cast(cast(#Invoice as bigint) as varchar) -->> Result : 1000010

SQL freetext() not working on some columns

I have a table which contains 7 columns and I have created a full text index on the table. However I noticed that a search using freetext() does not return any rows on 2 of the columns
Its returns rows on other columns.
Here is my query
select * from dbo.ModelCategoryValues
where freetext(economyvalues,'24,29')
and freetext(featurevalues,'10')
and freetext(pricerangevalues,'15')
and freetext(performancevalues,'18,20')
and freetext(economyvalues,'22,24')
and freetext(usevalues,'28')
This returns expected results
However when I run the below no rows are returned
select * from dbo.ModelCategoryValues
where freetext(cartypevalues,'1')
I can see rows corresponding to the above data. I have tried everything from re-populating index to re-creating it but no success.
FREETEXT by default excludes the records having "STOPLIST" Values.
To resolve the problem, set the StopList to 'OFF' using following query :-
ALTER FULLTEXT INDEX ON DealerSearch SET STOPLIST = OFF

Inserting one table's complete column data to a particular column in another table in SQL Server

Inserting one table's complete column data to a particular column in another table in SQL SERVER
I have two tables i.e AuditCalendar, ScheduleAudit
Audit Calendar has two columns Taskid, TaskTypeId
Schedule Audit has two columns Scheduleid, Taskid
Audit Calendar looks like this
Taskid (Auto increment) TaskTypeId
-------------------------------------
1 1
2 2
3 3
4 1
5 1
But I want Taskid column data from Audit Calendar table based on TaskTypeId .Columns
After completion of query, the ScheduleAudit table should look like this
Scheuleid (AutoIncrement) Taskid
-------------------------------------
1 1
2 1
3 1
I have to run this query seems to look like a error
Subquery returns more than one value
Query is:
INSERT INTO ScheduleAudit(TaskId)
VALUES ((SELECT TaskId FROM AuditCalendar Where TaskTypeId = 1))
Please can you suggest how I can do this approach I am new to SQL Server but someone says that use cursors.... I am really confused last 1 week on words. And also search google but not get it now...Please can you give me any one valuable suggestions.
insert ... values is supposed to insert a single row. So what you have in the parentheses is supposed to produce a single row, or else it would fail.
There's no need to use insert ... values, when you can use insert ... select:
INSERT INTO ScheduleAudit(TaskId)
SELECT TaskId FROM AuditCalendar Where TaskTypeId=1
...however, that would produce
1 1
2 4
3 5
I'm not sure I understand the logic behind producing your example output.

CANNOT create unique index in SQL Server, how to know what index is the index ID referring to

I have a script that gives an error when being executed:
Msg 1505, Level 16, State 1, Server CBR07I300FVA1, Line 1
CREATE UNIQUE INDEX terminated because a duplicate key was found for
index ID 17. Most significant primary key is '44'.
The statement has been terminated.
The script contains thousands of lines of queries so I have no idea where the error comes from in the script. Is there a way to know what "index ID 17" stands for?
Insert print statement before every significant step (say, create unique index) in the script and you're done.
It's usually done like this:
if ##error <> 0
PRINT '##error is ' + ltrim(str(##error)) + '.'
else
print 'Index IX_... successfully created'
You say a script with thousands of lines, eh?
My advise: put a print("Test") in the middle and see wether the error occurs before or after. And then again in the middle of the middle etc. until you find the place that is causing you the troubles.
The table you're working on already contains data; and the data isn't unique with regard to your new index.
Example:
col1 | col2 | col3
====================
foo | 1 | q
bar | 2 | w
bar | 3 | e
bar | 2 | r
In the above table, you couldn't create a unique index on (col1,col2), exactly because the data in it would be non-unique (multiple rows with (bar,2)). The script can't know which of those "duplicate" rows is actually needed. There are three options available to it:
create a UNIQUE index with duplicate rows (invalid, as it's not unique any more)
delete the duplicate rows (unsafe, how can it know which rows are needed?)
do nothing and throw an error (safest option, you are here)
What you can do to resolve this:
run a query to find duplicates - if you group the rows by those columns used by the index, some of the groups will have multiple rows. Those are your duplicates; you need to somehow eliminate their duplicity.
If you are running this script in SSMS, just double click on the error and it will take you to the line of code that has caused the error...

how to show last 3 records on the table if there are unknown number of record on sql sever

how to show last 3 record on the table if there are unknown number of records on sql sever.
for exam :
a table given a some records in which last 3 records show on table .
i have this query in which known records available :
"select * FROM ( select Emp_ID, Salery , dense_rank()over(order by Salery DESC)rn from e1) WHERE rn >1 " but i want to got unknown number of records given a table , will be find last 3rd number of records.

Resources