How to import a shape file into SQL Server? - sql-server

In the past I have used shape2sql to import shape files into SQL Server.
I tried that route again on a newer Windows 10 box, and nothing seems to happen when trying to use that application.
Any suggestions for either getting shape2sql to work, or alternative ways to upload shape files into SQL Server?

So, it turns out that ogr2ogr has the ability to import directly into a SQL Server database now. I created a temporary database and did the following:
Install ogr2ogr via the GDAL core MSI installer from http://www.gisinternals.com/release.php
Open a command line to the installed path
Run the following:
ogr2ogr -overwrite -f MSSQLSpatial -lco “GEOM_TYPE=geography” -a_srs
"EPSG:4326"
"MSSQL:server=.;database=temp_import;trusted_connection=yes" "[path to
shape file]"

I've managed to import successfully on remote server. Here goes procedure
Install QGIS Desktop 3.10.5..or whatever version is actual.
How to find out what goes to parameter -a_srs, in my casa that is : -a_srs "EPSG:3765":
Open QGIS Desktop 3.10.5, open new project and drag-drop your shape file to Layers left sidebar. Right click on that layer and go to Properties. There you will be able to read your -a_srs information.
Run command in CMD:
C:\Program Files\QGIS 3.10\bin>ogr2ogr -progress -f "MSSQLSpatial" "MSSQL:server=xxx.xxx.xxx.xxx,yyyy;database=yourDB;UID=yourUse;PWD=yourPassword" "C:\Users\Username\Desktop\ShapeFiles\Name_of_shape_file.shp" -a_srs "EPSG:3765" -lco PRECISION=NO
Where xxx.xxx.xxx.xxx - IP address of your server
yyyy - port number of your server if you are running SQL Server on different port than standard (I think you can omit that in case you are)
Output:
0...10...20...30...40...50...Warning 1: Ring Self-intersection at or near point 664451.64629999956 5053409.8562000087
60...70...80...90...100 - done.

Related

Run sqlserver to restore database on docker (bitbucket)

I want to mount an sql server on Bitbucket so I can download a db (bak file), restore it an upload it later on a main scheme on my server. I've tried:
Mount an SQLServer image, but didnt recognize the path to the downloaded file (problems with mssql tools)
Mount an SQLtool image, but didnt recognize the path too
Python 3.8 image and install sql server and sql tool, but systemctl wasnt found
How can I do this?

Create local SQL Server database from installation package for UWP

I'm trying to create an installation package that will create a local database for my UWP. Currently I'm using NSIS to create the package.
I developed my UWP with the idea that some users might want to use a tablet or phone from time to time, but the app will mainly be used on desktops and my installer is meant for desktop installations.
I first tried using the SQLLocalDB Utility , but I found out it's not supported in UWP. I eventually started using SQL Server media files to install SQL Server Express and I think that's the way I want to go (I want a local database that's a little more robust than SQLite and it seems to be my best option).
I don't know if my current issue is with NSIS or SQLCMD (which I use in the installer to build the database). Here's the part of my .nsi that I'm using to build the database:
OutFile "db_installer.exe"
!include psexec.nsh
!include LogicLib.nsh
Section
InitPluginsDir
SetOutPath $PLUGINSDIR
File /r SQLCMD
ExecWait '"$PLUGINSDIR\SQLCMD\sqlcmd.exe" -S Cmp-000000\SQLEXPRESS -i createdbonfile.sql -o output.txt'
FileOpen $4 "$PLUGINSDIR\SQLCMD\output.txt" r
FileSeek $4 0
ClearErrors
${DoUntil} ${Errors}
FileRead $4 $1
DetailPrint $1
${LoopUntil} 1 = 0
FileClose $4
# default section end
SectionEnd
At one point I got the installer to at least print the SQL errors. Since then, I thought I only removed some calls to "DetailPrint". Now, however, SQLCMD still creates the output file but doesn't put anything in it. And of course the biggest issue is that my SQL commands are not being executed.
I was able to successfully build the local database I wanted by using the SQL Server media files and the SQLCMD utility (as mentioned in the question). The issue I had was with my .nsi code. I thought that I was reading the output.txt file I created using SQLCMD in the intaller, but it turns out I was actually reading a file I made in a practice run and accidently loaded to the installer while compiling. With that realization, it soon came obvious that I was missing the paths to the files I was referencing. Here's the code again with the appropriate files paths:
OutFile "db_installer.exe"
!include psexec.nsh
!include LogicLib.nsh
Section
InitPluginsDir
SetOutPath $PLUGINSDIR
File /r SQLCMD
ExecWait '"$PLUGINSDIR\SQLCMD\sqlcmd.exe" -S Cmp-000000\SQLEXPRESS -i $PLUGINSDIR\SQLCMD\createdbonfile.sql -o $PLUGINSDIR\SQLCMD\output.txt'
FileOpen $4 "$PLUGINSDIR\SQLCMD\output.txt" r
FileSeek $4 0
ClearErrors
${DoUntil} ${Errors}
FileRead $4 $1
DetailPrint $1
${LoopUntil} 1 = 0
FileClose $4
# default section end
SectionEnd
You can connect to a remote SQL Server, for example on Azure. Creating LocalDB from UWP won't be supported, as it requires LocalDB to be installed on the target machine.
If you need a local database, then SQLite will be the best choice.

Unable to mount data directory in MSSQL RC1 server setup in linux

I'm trying to change the default data directory for MSSQL Server RC1 2017 after installation and setup in linux (Ubuntu-16.10).
I used the following command to set the default data directory. Then restarted the mssql server.
sudo /opt/mssql/bin/mssql-conf set filelocation.defaultdatadir /mnt/var/opt/mssql/data/
systemctl restart mssql-server.service
After this I tried to create a simple database "test"
sqlcmd -s localhost -U sa -P "someStrongPassword" -Q "CREATE DATABASE test"
The error returned is as follows:
MODIFY FILE encountered operating system error 31(A device attached to
the system is not functioning.) while attempting to expand the
physical file '/mnt/var/opt/mssql/data/test.mdf'.
CREATE DATABASE
failed. Some file names listed could not be created. Check related
errors.
The error log indicates an OS error:
/mnt/var/opt/mssql/data/test.mdf: Operating system error 31(A device
attached to the system is not functioning.) encountered.
I cannot mount the data directory by any means. The permissions to "/mnt" directory are set to 777 too. Changing the default data directory to any other folder, works perfectly fine. Is this a known or recent bug with mssql server?
Yes, there is an issue with using remote storage through NFS and SMB that came up in CTP 2.1 and was not yet fixed in RC1. See release notes: https://learn.microsoft.com/en-us/sql/linux/sql-server-linux-release-notes#a-idrc1-rc1-july-2017-a
The only workarounds are to use local storage or CTP 2.0. We are working on a fix. Release ETA is TBD ATM.

Deploy the database to Docker Container microsoft/mssql-server-linux

I have a database running on SQL Server (13.01) on Windows. I like to deploy it to the Docker Container on Linux using SSDT.
I can perfectly connect to the server running on Docker and create/drop database manually and play with the data.
The problem is I can not publish it. I'm executing following script on Powershell
PS: SqlPackage.exe /Action:Publish /SourceFile:"d.dacpac" /TargetConnectionString:"server=containeraddress;database=thedatabase;user id=sa;password=thepassword;
and getting the following error.
Unable to connect to master or target server 'the database'. You must have a user with the same password in master or target server 'the database'. (Microsoft.Data.Tools.Schema.Sql)
I have the same user and same password on target and source servers.
Is there anybody has the same problem and know how to solve it?
I'll post this here as most of the answers refer to having an existing compiled dacpac file, which may not always be possible. I haven't seen similar ideas posted elsewhere to the solution I'm suggesting here.
Given your usage of docker and if you wish to compile your visual studio project inside the container, given certain combinations of the container base OS and image may not be possible to create a dacpac file with msbuild.
You can work around restoring the database using a series of unix based commands, taking note that the visual studio database project is usually just a series of SQL files, below I show an example of this, where I concat the SQL files into a single file and call sqlcmd to run the script;
FROM mcr.microsoft.com/mssql/server
WORKDIR /init
ENV ACCEPT_EULA=Y
ENV MSSQL_SA_PASSWORD=MyPassword
EXPOSE 1433:1433
RUN apt-get update && apt-get install dos2unix
COPY /solution_folder/database/Tables/*.sql /init/
WORKDIR /database
RUN echo "CREATE DATABASE [database_name];\nGO\nUSE [database_name];\n” >> /database/create.sql
RUN for f in /init/*.sql; do dos2unix $f; cat $f >> /database/create.sql; echo "\nGO\n" >> /database/create.sql; done
RUN ( /opt/mssql/bin/sqlservr --accept-eula & ) | grep -q "Service Broker manager has started" && /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P ‘MyPassword’ -i /database/create.sql && pkill sqlservr
The reason for "dos2unix" is that the SQL files created within visual studio have unique hidden cr/lf (and other characters) which the linux version of sqlcmd won't interpret successfully and will cause errors (which is kind of bizarre and this is exactly the kind of thing you'd want a cross platform database to be able to cope with)
Also, within the final run command you have to start-up the sql server service temporarily otherwise you'll also get errors; it's a little bit of work-around, and a bit fiddly and I'm not sure entirely that the microsoft sql server linux container is well designed enough for the simple task of restoring a database like this, the nuances are the differences between building and running a container and needing some sort of happy middle ground of both concepts for it to work.
Given here isn't a complete solution to restore, it only deals with Tables from the project file, although it should be trivial to expand to scalar functions and stored procedures.
Which version of SqlPackage.exe are you using? Only the most recent release candidate versions of SqlPackage.exe support SQL Server vNext CTP. The SqlPackage release candidate can be downloaded here: https://www.microsoft.com/en-us/download/details.aspx?id=54273

Connect to database in App_Data folder

Is it possible to connect to a database in the App_Data project folder using the command line? I'm trying to test some SQL statements outside of the VS environment.
I tried using the below, but that didn't seem to work:
sqlcmd -S .\Sqlexpress -d [location of mdf]
Not sure about the app_data folder, but if you have the db files running off of a sql install, you can use sqlcmd command line which is installed with the sql client tools or the sql feature pack (sql native client).

Resources