This function will return a list of all dates between a start date and an end date.
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: bryan valencia -- Create date: 11/20/2018 -- Description: returns the dates between start and end (inclusive). -- ============================================= CREATE FUNCTION DaysBetween(@startdate DATE, @enddate DATE) RETURNS @calendar TABLE ( calendarday DATE PRIMARY KEY ) AS BEGIN -- Fill the table variable with the rows for your result set
WITH calendar AS ( SELECT CAST(@startdate AS DATETIME) DateValue UNION ALL SELECT DateValue + 1 FROM calendar WHERE DateValue + 1 <= @enddate )
insert into @calendar(calendarday) ( SELECT cast(DateValue as Date) calendarday FROM calendar ) OPTION (MAXRECURSION 0)
RETURN END GO
To call it: SELECT * FROM [dbo].[DaysBetween] ('01 jan 2018', '01 feb 2018')
...
Bryan Valencia is a contributing editor and founder of Visual Studio Journey. He owns and operates Software Services, a web design and hosting company in Manteca, California.
This article explains how to copy a database from one server to another when Backup/Restore is not an option.
This will work as long as you have SQL Server Management Studio access to the source and destination databases.
This is very slow, and may take many hours to run on a big database.
First, open the database server you want to copy in SSMS.
Next, Right-click the database, Tasks -> Generate Scripts.
In my case I want all the scripts to completely recreate my database, so here are the steps.
Next
Script entire database and all database objects
Save to new query window (file is OK too).
Click Advanced (by default it will write a script to create all objects, but not copy data). Make sure to select Schema and data.
Review all the other settings to make sure you get the stuff you want on the destination database. Click OK, then Next.
Review and Next (or previous if you need to change something.)
You'll get a checklist screen and then the script is done. It should pop up in SSMS.
To restore the database...
Log SSMS into the destination database.
Back up what's there if there is any chance you'll need it.
In your script, if you need to rename the database to satisfy the new hosting company, do a global replace in the SQL script. Make sure the FileNames reflect the destination database name not the source database
Copy the entire script to the clipboard.
right click the destination database and do a New Query (if the database hasn't been created yet, you can click the server name).
Paste the script into the new query editor.
If the database already exists, you may need to delete the CREATE statement and all the ALTER DATABASE statements, down to the [USE database] command.
Look in the scrollbar for possible problems... Then fix them.
Run the script and see what happens. Get coffee. It's gonna be a while. Call your Dad. He misses you.
You're done. Just in case you don't have all the same Generate Scripts options, this demo was from a SQLExpress2016 database, using all this stuff...
Microsoft SQL Server Management Studio 14.0.17199.0
Microsoft Analysis Services Client Tools 14.0.1008.227
Microsoft Data Access Components (MDAC) 10.0.16299.15
I got a job from a client and it was a fixed bid. We pay you x dollars and you finish the work.
BUT...
The scope was ill-defined
the "spec" for the project was "replace our existing system", not an objective set of criteria. Therefore any variation between their old system and the new one could be considered incorrect.
If there are features that we cant see from the 300 screen shots we have, then we'll have to do them all to be "finished"
because there is no objective spec, this will end up where all fixed bid contracts end up
the client will expect us to "finish" the project forever, because if they ever accept the work, then they will have to pay more for changes.
the client will always have a giant list of 'fixes' whenever we show them the completed work.
we will continue to push them to go live, and they will have every inclination to manufacture more and more changes, even if they are not in the original project.
The estimate was based on a very simplified version of the work
we showed the client a mockup with all the bells and whistles, and gave them an estimate.
the client liked it, but wanted the work much, much cheaper.
we offered a simplified approach to lower the cost.
they didn't really listen and kept expecting everything at the lower cost.
This week I will finish the project. I will build it to the demo server and the client will look at it. We have exhausted ALL the hours assigned to the work. They are going to have a list of things they want "fixed" before going live. At that point we will want more money, but as far as they are concerned, it's a fixed bid. They get whatever they want for the agreed price.
Welcome to slavery. Working free forever and never getting paid again.
Unless a project is less than 10 hours, NEVER accept a fixed bid contract. It's a no-win scenario.
...
Bryan Valencia is a contributing editor and founder of Visual Studio Journey. He owns and operates Software Services, a web design and hosting company in Manteca, California.
Recently I needed to add a quick updater that added a value to Table1
when someone copied data from Table2.
Now, the normal way to do this with Entity Framework is to load the
Entity, and change the value, then save the changed entity.
But I am a SQL guy from way back, and rather than fetch the entire
record and write it all back through Entity Framework, I figured it
would be Way more efficient if I just sent the database an update
directly. Turns out that for us old SQL developers, it's terribly easy
to do just that.
Here is my example subroutine, for your dining pleasure.
/// /// Updates a Toolkit with the number of the Assignment ID Created from it. If you are trying to remove an assignment id from a toolkit, use RemoveAssignmentFromToolkit() /// /// The toolkit ID to be updated /// The AssignmentID to attach public static void UpdateToolkitWithAssignmentID(int ToolkitID, int AssignmentID) { using (ORM db = new ORM()) { try { db.Database.ExecuteSqlCommand( "UPDATE [dbo].[ToolkitRequests] SET[AssignmentID] = @AID Where[UniqueId] = @ID", new SqlParameter("@AID", AssignmentID), new SqlParameter("@ID", ToolkitID) ); } finally { db.Dispose(); } } }
That's it! It's super fast, and there is no mucking about in Entity
Framework. NOTE that all the EF validity checking is skipped when you
do this, so use it in cases where it's a simple SQL and you can ensure
data integrity yourself. In my case, there is no way to get in this
routine without valid values in both params, so I know I won't get
surprised with a stray NULL.
---
This email has been checked for viruses by Avast antivirus software.
This gives a list of all the tables and columns in the tables in your database. The TABLES table was included to exclude views.
Select COLUMNS.TABLE_SCHEMA, COLUMNS.TABLE_NAME, COLUMNS.COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS inner join INFORMATION_SCHEMA.TABLES on Tables.TABLE_CATALOG = Columns.TABLE_CATALOG and Tables.TABLE_SCHEMA = Columns.TABLE_SCHEMA and Tables.TABLE_NAME = COLUMNS.TABLE_NAME and Tables.TABLE_TYPE = 'BASE TABLE'
...
Bryan Valencia is a contributing editor and founder of Visual Studio Journey. He owns and operates Software Services, a web design and hosting company in Manteca, California.