The postings on this site are my own and do not represent my Employer's positions, advice or strategies.

LifeAsBob - Blog

 

Home

No Ads ever, except search!
Wednesday, May 18, 2022 Login
Public

Postgres to SQL Quick Tips3/4/2022 7:23:55 AM
Very First make sure your working with a great Postgres DBA.

1.  Convert the schema from Postgres to SQL Server
2.  Install ODBC Drivers 
3.  Use SSIS or it's watered down cousin DTSWizard
4.  If really necessary you can drop down to custom code to transfer data.
5.  Edit the ProvidersDescriptors.xml
6.  Out of Memory Reading tuples ?  (add UseDeclareFetch=1 to connect string).

For data type compatibility
https://www.convert-in.com/docs/mss2pgs/types-mapping.htm 
 
For:  sql server odbc postgres column attribute colum_size is not valid providerdescriptors.xml
MaximumLengthColumnNameNumericPrecisionColumnName, and NumericScaleColumnName attribute values to "LENGTH""PRECISION", and "SCALE", respectively.


The column attribute "COLUMN_SIZE" is not valid.

The column attribute "DECIMAL_DIGITS" is not valid.

The column attribute "COLUMN_SIZE" is not valid.


<dtm:ColumnSchemaAttributes
    NameColumnName = "COLUMN_NAME"
    OrdinalPositionColumnName="ORDINAL_POSITION"
    DataTypeColumnName = "TYPE_NAME"
    MaximumLengthColumnName = "COLUMN_SIZE"
    NumericPrecisionColumnName = "COLUMN_SIZE"
    NumericScaleColumnName = "DECIMAL_DIGITS"
    NullableColumnName="NULLABLE"
    NumberOfColumnRestrictions="4"
/>

... to ...

<dtm:ColumnSchemaAttributes
    NameColumnName = "COLUMN_NAME"
    OrdinalPositionColumnName="ORDINAL_POSITION"
    DataTypeColumnName = "TYPE_NAME"
    MaximumLengthColumnName = "LENGTH"
    NumericPrecisionColumnName = "PRECISION"
    NumericScaleColumnName = "SCALE"
    NullableColumnName="NULLABLE"
    NumberOfColumnRestrictions="4"
/>
sql server postgresql out of memory while reading tuples 
"Driver={PostgreSQL Unicode};Server=ip;Port=port;Database=db_name;Uid=user;Pwd=pass;UseDeclareFetch=1"
https://stackoverflow.com/questions/22532149/vba-and-postgresql-connection


Stored procedure can not return BIGINT11/2/2021 2:02:07 PM
SQL Server stored procedures can not return a big integer.

Table has identity value with primary key as big integer.

Stored procedure is running Return Scope_Identity() which works until the identity values exceeds the value of the implicit integer conversion, 2,147,483,646.

This is documented, but rarely run into.

The correct best practice is too use an output parameter for the stored procedure.
This requires changing the procedure and calling code, which can be difficult if this happens in production environment during a busy period.

The temporary solution is too reset the identity value to a negative values, as the range of an integer, -2,147,483,646 to +2,147,483,646.

Watch when you do this, as checkident('tablename') will reset an identity value, so be sure to use noreseed as part of the command.

For monitoring purposes:

dbcc checkident ([table name],reseed,-2147483646)
select max([column identity]),min([column identity]) from [table name] with (nolock)
select count(*) from [table name] with (nolock) where [column identity] < 0


 

and some discussion on the work-around, which is too return the value as an output parameter.

 

https://stackoverflow.com/questions/25915653/return-bigint-in-a-stored-procedure




Grant Create Table Permissions to a Schema, but not DBO11/2/2021 1:48:31 PM

Granting Create Table permissions to a specific schema requires, granting create table at the database level and alter at the schema level (doing no grants to the dbo schema).

Create user [some user] from external provider
go

Grant alter,select,insert,update,delete on schema::[schema name] to user [some user]
go

-- if control or references is needed
-- grant control,references on schema:: [schema name] to user [some user]

grant create table, create view, create function,create type to [some user]
go

Always confuses me to remember the grant alter and the create table portions.

SQL Server Walk the Dog8/18/2021 6:07:33 AM

Recently I was given several text files with 500k to 1 million update statements in each that needed to be run.

Try loading and running this in SQL Server Management studio and you'll find that if it does load and run, sometimes it fails part through with memory error or other issues (all related to the client, not the engine).

It would have been better for the teams to work together and write this as a table join update as opposed to generating so many individual statements, but that ship had sailed and now these needed to be run.

I ended up "Walking the Dog" or RBAR (Row by agonizing Row).

Load the text files to a table, use a cursor to read each row and dynamically execute it.  I threw in a global counter i could query to figure out where it was at in the process.  In total it took about 2-3 hours to run.

Here is how to walk the dog.

SET NOCOUNT ON; 

  

DECLARE @sqlstmt nvarchar(4000)

 

create table ##Global_Count (current_rownum int)

insert into ##Global_Count values (0)

 

 

PRINT '-------- starting --------'; 

  

DECLARE cur_statements CURSOR FOR  

SELECT sqlstmt FROM DocumentId_3

  

OPEN cur_statements 

  

FETCH NEXT FROM cur_statements INTO @sqlstmt

 

WHILE @@FETCH_STATUS =

BEGIN 

       Exec sp_Executesql @sqlstmt

       update ##Global_Count set current_rownum = current_rownum + 1

FETCH NEXT FROM cur_statements INTO @sqlstmt

END

 

Close cur_statements

Deallocate cur_statements

 

select * from ##Global_Count

drop table ##Global_Count


SQL Server Drop Constraints unknown name8/12/2021 6:01:02 AM
SQL Server will create constraints with a default name, that is not consistent across implementations if you do not specify a name.

Best practice would be to always specify a name.

But if you inherit a database across 1000's of locations and have to write update scripts, you may come across the need to drop a constraint for a column when you don't know the constraint name.

This script will find the constraint name based off the column and dynamically generate a drop statement for it.

SET @SelectStatement =  (SELECT 'ALTER TABLE [Machines] DROP CONSTRAINT ' + name

                                   FROM sysobjects               

                    WHERE parent_obj = object_id('TableName') AND xtype = 'D' AND name LIKE '%ColumnName%')

                           EXECUTE sp_executesql @SelectStatement



Loblolly Pine Trees7/13/2021 5:06:26 AM
Trying to grow some Loblolly Pine Trees in Missouri.
Seedling sourced from my Dad in South Carolina, and transplanted to larger containers, once they are big enough I'm going to plan them in a line and make a boundary at the back of my property.




Sidewalk replacement7/10/2021 8:44:09 AM
The sidewalk below my front step had collapsed down about 3 inches.  This leads to water running under the steps and that's bad as it puts water close to the foundation of the house, and even worse, snakes had decided to live under there (look in the pics and you'll see the snake eggs).