Assign start value explicitly to identity column - tsql

I have below 2 tables (sybase ASE 15):
TotalTradeCount = [count (int) ,client (varchar) ] => (325689,"XXX")
TodaysTrade = [ count (int,**identity**), Client, Tradetype,.... ]
How do I assign TodaysTrade..count = 325690 as a start value explicitly so that subsequent entries will have auto-incremental "count" by 1.
Without using sp_chgatttribute and set identity_insert on as these require admin rights (sa role) which we don't have.

Related

Poltergeist using FromSqlRaw with EF Core5

I have to retrieve this data from 2 different databases within the same instance
For which I put that same SQL statement in my code
public int ImportarUTEs()
{
try
{
int registrosAñadidos = 0;
var registrosSAP = _contextSAP.Licitadores
.FromSqlRaw(#"select distinct ot.IDLICITADOR as IdLicitador,
l.cardcode as CodigoSAP,
ic.cardname as Nombre
from ofertantes ot INNER JOIN licitadores l on ot.idlicitador=l.idlicitador
inner join ofertas o on o.codigoanalizada=ot.codigoanalizada
inner join Fulcrum.dbo.OCRD ic on l.cardcode=ic.cardcode collate SQL_Latin1_General_CP1_CI_AS
where year(o.fechapres)>=2015 AND
ot.idlicitador in(
select IDLICITADOR from LICITADORES
GROUP by IDLICITADOR
HAVING COUNT(*)>1
)
order by IdLicitador, CodigoSAP")
.ToList();
But what is my surprise when I see the result obtained
When you have to obtain the 2 records corresponding to LicitasorID 2368, I see that I get 3 records where [8] repeats the value of [6] and instead of being the value corresponding to LicitadorID 2881 and CodigoSAP 430FULCRUM, it assigns the value LicitadorID 2368. But the strangest thing is that when it is time to collect the values of IdLicitador 3150 it turns out that it does the same thing, IdLicitador 3150 and CodigoSAP 430FULCRUM the [10 ] turns it into IdLicitador 2368 and CodigoSAP 430FULCRUM.
That is, for some reason that I can't understand the value obtained in the EF Core 5 project is not the same as the one obtained in the SQL Server instance and I can't think of what to do about it
Any idea, please?
Thanks
The problem was to define the primary key in LicitadoresSAP entity
modelBuilder.Entity<LicitadorSAP>()
.HasKey(c => new { c.IdLicitador, c.CodigoSAP });
Now works fine

Couchbase Update query divide

I am trying to update the document using the UPDATE query statement on the couchbase.
EX)
UPDATE Users SET cityIndex = 1 where Users.city= "NewYork";
There was so much data that I wanted to divide 3,000 to 4,000 and proceed with the UPDATE. How should I proceed?
There is PRIMARY INDEX.
The Eventing Function method that vsr alluded too is quite simple (7 lines sans comments) and you run it as a one-off point tool deploying it from Everything. Note there is no need for any index for this to work.
// To run configure the settings for this Function, UpdateAllCityIndex, as follows:
//
// Version 7.0+
// "Listen to Location"
// bulk.data.yourcollection
// "Eventing Storage"
// rr100.eventing.metadata
// Binding(s)
// 1. "binding type", "alias name...", "bucket.scope.collection", "Access"
// ---------------------------------------------------------------------------
// "bucket alias", "src_col", "bulk.data.Users", "read and write"
//
// Version 6.X
// "Source Bucket"
// yourbucket
// "MetaData Bucket"
// metadata
// Binding(s)
// 1. "binding type", "alias name...", "bucket", "Access"
// ---------------------------------------------------------------------------
// "bucket alias", "src_col", "Users", "read and write"
//
// For more performance set the workers to the number of physical cores
function OnUpdate(doc, meta) {
// only process documents with the city field
if (!doc.city) return;
// only update New York if cityIndex isn't already 1 or does not exist
if ( doc.city === "NewYork" && (!doc.cityIndex || doc.cityIndex !== 1 )) {
doc.cityIndex = 1;
// write back the updated doc via the alias
src_col[meta.id] = doc;
}
}
Option 1)
You can use couchbase eventing
case 2 of https://docs.couchbase.com/server/current/eventing/eventing-example-data-enrichment.html
https://docs.couchbase.com/server/current/eventing/eventing-examples.html
Option 2)
CREATE INDEX ix1 ON Users (city, cityIndex);
UPDATE Users AS u
SET u.cityIndex = 1
WHERE u.city = "NewYork" AND u.cityIndex != 1
LIMIT 4000;
Using a primary index, you can issues multiple queries on a (presumably stable primary index) and iterate over it. Little bit more complicated, but generalized.
rq is the bucket, s is the scope, t1 is the collection.
create collection rq.s.t1;
create primary index on rq.s.t1;
First query:
UPDATE rq.s.t1 USE KEYS [(
SELECT META().id
FROM rq.s.t1
ORDER BY META().id
LIMIT 10)] SET x = 1 RETURNING MAX(META().id);
Second to N query until you're done (nothing gets returned):
Take the max value of meta().id from the previous query (see the WHERE clause)
UPDATE rq.s.t1 USE KEYS [(
SELECT RAW META().id
FROM rq.s.t1
WHERE META().id > "007dd444-fa39-498f-b070-6cd0d41abe3d"
ORDER BY META().id
LIMIT 10)] SET x = 1 RETURNING META().id;
You can optimize this loop by setting the initial meta().id to compare against "".

How do I use the Class::DBI->sequence() method to fill 'id' field automatically in perl?

I'm following the example Class::DBI.
I create the cd table like that in my MariaDB database:
CREATE TABLE cd (
cdid INTEGER PRIMARY KEY,
artist INTEGER, # references 'artist'
title VARCHAR(255),
year CHAR(4)
);
The primary key cdid is not set to auto-incremental. I want to use a sequence in MariaDB. So, I configured the sequence:
mysql> CREATE SEQUENCE cd_seq START WITH 100 INCREMENT BY 10;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT NEXTVAL(cd_seq);
+-----------------+
| NEXTVAL(cd_seq) |
+-----------------+
| 100 |
+-----------------+
1 row in set (0.00 sec)
And set-up the Music::CD class to use it:
Music::CD->columns(Primary => qw/cdid/);
Music::CD->sequence('cd_seq');
Music::CD->columns(Others => qw/artist title year/);
After that, I try this inserts:
# NORMAL INSERT
my $cd = Music::CD->insert({
cdid => 4,
artist => 2,
title => 'October',
year => 1980,
});
# SEQUENCE INSERT
my $cd = Music::CD->insert({
artist => 2,
title => 'October',
year => 1980,
});
The "normal insert" succeed, but the "sequence insert" give me this error:
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near ''cd_seq')' at line
1 [for Statement "SELECT NEXTVAL ('cd_seq')
"] at /usr/local/share/perl5/site_perl/DBIx/ContextualFetch.pm line 52.
I think the quotation marks ('') are provoking the error, because when I put the command "SELECT NEXTVAL (cd_seq)" (without quotations) in mysql client it works (see above). I proved all combinations (', ", `, no quotation), but still...
Any idea?
My versions: perl 5.30.3, 10.5.4-MariaDB
The documentation for sequence() says this:
If you are using a database with AUTO_INCREMENT (e.g. MySQL) then you do not need this, and any call to insert() without a primary key specified will fill this in automagically.
MariaDB is based on MySQL. Therefore you do not need the call to sequence(). Use the AUTO_INCREMENT keyword in your table definition instead.

Create One Column from Six different Columns Where all the columns for a row contain a null value except for one

The table includes a natural key in the form of a project number.
Use a CASE statement:
SELECT
CASE WHEN EST = 1 THEN 'EST'
WHEN INI = 1 THEN 'INI'
WHEN AD = 1 THEN 'AD'
WHEN DEV = 1 THEN 'DEV'
WHEN UAT = 1 THEN 'UAT'
WHEN IMP = 1 THEN 'IMP'
END AS [Status]
FROM table

SSRS 2008: How to create parameter based on another parameter

I know others have asked similar questions, but I have tried their solutions and it still is not working for me.
I have one parameter called "Region" which uses the "region" dataset and another report parameter called "Office" which uses the "office" dataset.
Now I want "Office" list of values to filter based on "Region" selection. Here is what I did so far. For the region dataset, it returns "regions_id" and "region_description". Then for "Region" report parameter, I selected "Text" datatype and allow Null values. This may be a mistake to select "text" since this is a uniqueidentifier value. For available values, I selected the region dataset and regions_id for value, region_description for label. I went to Advanced tab and selected "Always refresh". And on Default tab, I entered "(Null)", for when they want to see all regions.
NExt, I created a report parameter called "regions_id2", allow null values, and I set available values = region dataset. For values and label both, I specified the regions_id. For default value, I again entered "(Null)". And I again selected "Always refresh".
Finally, I added this "regions_id2" parameter to the "office" dataset. And then the office report parameter uses the "office" dataset with available values. Value field = "group_profile_id" and label field = "name_and_license". Default values = "(Null)". Advanced "Always refresh".
And I ordered these report parameters in this same order: Regions, regions_id2, and Office. But now when I run this report I get no errors, however, the list of offices includes all of the offices regardless of what I choose for regions. Here is my T-SQL for these datasets:
CREATE Procedure [dbo].[rpt_rd_Lookup_Regions]
(
#IncludeAllOption bit = 0,
)
As
SET NOCOUNT ON
If #IncludeAllOption = 1
BEGIN
Select Distinct
NULL AS [regions_id],
'-All-' AS [region_description]
UNION ALL
SELECT Distinct
[regions_id],
[region_description]
FROM [evolv_cs].[dbo].[regions]
Where [region_description] not in ('NA','N/A')
Order By [region_description]
END
Else
BEGIN
SELECT Distinct
[regions_id],
[region_description]
FROM [evolv_cs].[dbo].[regions]
Where [region_description] not in ('NA','N/A')
Order By [region_description]
END
CREATE Procedure [dbo].[rpt_rd_Lookup_Facilities]
(
#IncludeAllOption bit = 0,
#regions_id uniqueidentifier = NULL
)
As
SET NOCOUNT ON
If #IncludeAllOption = 1
BEGIN
Select
Null As [group_profile_id],
Null As [profile_name],
Null As [license_number],
Null As [other_id],
--Null As [Regions_id],
'-All-' As [name_and_license]
UNION ALL
SELECT
[group_profile_id],
[profile_name],
[license_number],
[other_id],
--[regions_id],
[profile_name] + ' (' + LTRIM(RTRIM([license_number])) + ')' As [name_and_license]
FROM [evolv_cs].[dbo].[facility_view] With (NoLock)
Where [is_active] = 1 and (#regions_id is NULL or #regions_id = [regions_id])
Order By [profile_name]
END
Else
BEGIN
SELECT
[group_profile_id],
[profile_name],
[license_number],
[other_id],
[regions_id],
[profile_name] + ' (' + LTRIM(RTRIM([license_number])) + ')' As [name_and_license]
FROM [evolv_cs].[dbo].[facility_view] With (NoLock)
Where [is_active] = 1 and (#regions_id is NULL or #regions_id = [regions_id])
Order By [profile_name]
END
What could I possibly be doing wrong?
I fixed this by selecting the region parameter value from region dataset for the office dataset