Posts

Showing posts from October, 2020

SQL Server, How to easily create a text file with the result of your query (another easy tips)

Image
Hello friends, Its the time for another " useful but fast & easy ” tips! The question of today is:   I have a query, a select on my database the "give me" a resultset which I need to export to a text file. How to produce the text file? maybe this operation must be schedulable. So, How to do it?  We can take advantage of the SqlCmd command that we have already used here: execute T-SQL commands from the command line   Just prepare your select as in the picture below and save it into a .SQL file.     Then open a command promp and type: SqlCmd -U xx -P yyyyy -i select.sql -o c:\dati.txt where    -U username -P password  -i the location of your script   -o is the file name that will be filled up with your data!     Et voilà!    That's all for today I wish you a great day! Luca Previous post: SQL SERVER, One thing you should definitely know about the UPDATE statement ...  

SQL SERVER, One thing you should definitely know about the UPDATE statement ...

Image
Hello friends, Today, waiting for the saturday, we talk about a simple thing that you really should know about the UPDATE statement . How many times have you seen updates done like this?    UPDATE ORDRIG SET NeatPrice = Price * (100-PercDiscount)/100, total = NeatPrice * Qty where id = XX   Notice that the result of the second field depends on the result of the first one . So in this case order matters. How does SQL Server behave in this case? Seems difficult but it is more easy than you could image!   Just learn this easy rule: The UPDATE does not see the results of its work.   Of course you can tray by yourself doing this simple update: CREATE TABLE #ORDRIG ( ID INT IDENTITY ( 1 , 1 ), PRICE FLOAT , NETPRICE FLOAT ) INSERT INTO #ORDRIG ( PRICE , NETPRICE ) VALUES ( 0 , 0 ) UPDATE #ORDRIG SET PRICE = 5 , NETPRICE = PRICE SELECT * FROM #ORDRIG   Et voilà! That's all for tod...

SQL Server, How to execute T-SQL commands from the command line (attention this post may contain easy but useful tips 😏 )

Image
Hello friends, You know, i love “ useful but fast & easy ” tips! The question of today: Have you ever needed to run T-SQL commands from the command line? Yes? So, How to do it? Well, an easy way is to use the sqlcmd command. But what is the SqlCmd? The SqlCmd is a command line utility that is part of the standard installation of SQL Server and that you can run from the command prompt. First you need to prepare a script that contains the T-SQL commands to execute. In my example i saved myscript.sql in C:\TEMP folder. Second , just open the command prompt   And type in: sqlcmd -U "XX" -P "YYYY" -S "ZZZZ" -i “c:\temp\MyScript.sql”   You need to specifiy only the login information using: -U username -P password -S the server name -i the location of your script           Et voilà!  That's all for today! Luca Previous post: SQL Server, How to Read a list of files in a folder and compare its name with the value stored into a column...

SQL Server, How to Read a list of files in a folder and compare its name with the value stored into a columns

Image
Hello friends and welcome back! Today, first of all I would like to say a big thank you to all of you! We have reached 4000 friends ! 😀😀😀 (…I don't like to call "followers”) Today we will talk about a practical case: how to compare the names of files contained in a folder with the same ones indicated in a field of a database Let’s go! What is the problem we want to solve today? Suppose having a list of files in a window folder. If you customer would ask you to see where a filename read from the image column existing on the filesystem? So we have a window image folder:   We have also a table with a column image. In the image column we have the path of our images! How to do it directly with just 3 T-SQL commands? Easy... Step 1  - declare a table variable @name DECLARE @name TABLE (FileName nvarchar(500),depth int,isFile int) Step 2 - insert the file list into the table variable @name   INSERT INTO @name EXEC XP_DIRTREE ' ...